Found 9150 Articles for Object Oriented Programming

How to leave space with EmptyBorder in Java Swing

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

549 Views

Llet us first create a JPanel and set titled border:JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createTitledBorder("Demo Panel"));Now to create Empty Border:JPanel panel2 = new JPanel(new BorderLayout()); panel2.add(panel, BorderLayout.CENTER); panel2.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));The following is an example to leave space with EmptyBorder in Java Swing:Exampleimport java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JPanel panel = new JPanel();       panel.setBorder(BorderFactory.createTitledBorder("Demo Panel"));       JPanel panel2 = new JPanel(new BorderLayout());       panel2.add(panel, BorderLayout.CENTER);       panel2.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));       ... Read More

Java Program to create rounded borders in Swing

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

6K+ Views

Let us first create a Frame:JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setUndecorated(true);Now, create rounded borders:double x = 50; double y = 50; frame.setShape(new RoundRectangle2D.Double(x, y, 100, 100, 50, 50));The following is an example to create rounded borders in Swing:Exampleimport java.awt.geom.RoundRectangle2D; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo extends JPanel {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.setUndecorated(true);       double x = 50;       double y = 50;       frame.setShape(new RoundRectangle2D.Double(x, y, 100, 100, 50, 50));     ... Read More

Java Program to add Titled Border to Panel in Swing

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

265 Views

To set Titled Border to Panel, let us first create a Panel for our Java Swing Application:JPanel panel = new Jpanel(new BorderLayout());Now, set the titled border:panel.setBorder(new TitledBorder("Displaying Titled Border"));The following is an example to add Titled Border to Panel in Swing:Exampleimport java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.TitledBorder; public class SwingDemo {    public static void main(String[] args) {       JPanel panel = new JPanel(new BorderLayout());       panel.setBorder(new TitledBorder("Displaying Titled Border"));       panel.add(new JButton("Demo Button"), BorderLayout.SOUTH);       JOptionPane.showMessageDialog(null, panel);    } }OutputRead More

How to set fullscreen mode for Java Swing Application?

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

6K+ Views

To set fullscreen mode for your Java Swing application, use the setFullScreenWindow() method:GraphicsDevice device = graphics.getDefaultScreenDevice(); JFrame frame = new JFrame("Fullscreen"); device.setFullScreenWindow(frame);The following is an example to set fullscreen mode for Java Swing Application:Exampleimport java.awt.Color; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       GraphicsEnvironment graphics =       GraphicsEnvironment.getLocalGraphicsEnvironment();       GraphicsDevice device = graphics.getDefaultScreenDevice();       JFrame frame = new JFrame("Fullscreen");       JPanel panel = new JPanel();       JLabel label = new JLabel("", JLabel.CENTER);     ... Read More

Make a custom cursor appear when the user moves the mouse over some text in a Java Swing JDialog

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

720 Views

At first set the label on which you want the custom cursor to be visible on hover:JLabel label = new JLabel("Demo text! Hand cursor is visible on hover...");Now, set cursor to be visible as Hand Cursor instead of the default Cursor:label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));The following is an example to make a custom cursor appear when the user moves the mouse over some text:Exampleimport java.awt.Cursor; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class SwingDemo extends JFrame {    private void ShowDialog() {       JLabel label = new JLabel("Demo text! Hand cursor is visible on hover...");       label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));   ... Read More

How do you get the font metrics in Java Swing?

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

552 Views

To get the font metrics, use the FontMetrics class:Graphics2D graphics = (Graphics2D) gp.create(); String str = getWidth() + "(Width) x (Height)" + getHeight(); FontMetrics m = graphics.getFontMetrics();Now to display it:int xValue = (getWidth() - m.stringWidth(str)) / 2; int yValue = ((getHeight() - m.getHeight()) / 2) + m.getAscent(); graphics.drawString(str, xValue, yValue);The following is an example to get the font metrics in Java Swing:Exampleimport java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Font Metrics");       ... Read More

Java Program to create Arrow Button positioning North

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

259 Views

To create Arrow Button at position north, use BasicArrowButton:BasicArrowButton arrow = new BasicArrowButton(BasicArrowButton.NORTH);Above, we have set the arrow to NORTH. Now add it to Panel:panel.add(arrow, BorderLayout.NORTH);The following is an example to create Arrow Button positioning North:Exampleimport java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.plaf.basic.BasicArrowButton; public class SwingDemo extends JPanel {    public SwingDemo() {       setLayout(new BorderLayout());       JPanel panel = new JPanel(new BorderLayout());       add(panel, BorderLayout.EAST);       BasicArrowButton arrow = new BasicArrowButton(BasicArrowButton.NORTH);       panel.add(arrow, BorderLayout.NORTH);    }    public static void main(String[] args) {       JFrame frame = ... Read More

How to change display mode with Java Swings

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

1K+ Views

To change display mode with Java Swings, use the setDisplayMode() method. Here, we have set the Display mode as:new DisplayMode(800, 600, 32, 60));Now, when you will run the program, the frame would be visible in a different resolution than the actual set resolution of your system.The following is an example to change display mode with Java Swings:Exampleimport java.awt.DisplayMode; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setSize(800, 600);       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       GraphicsDevice graphics = GraphicsEnvironment.getLocalGraphicsEnvironment()   ... Read More

Java program to sort string stream with reversed comparator

Krantik Chavan
Updated on 23-Nov-2024 03:58:06

626 Views

In this article, we will learn how to sort a stream of strings using a reversed comparator in Java. Java 8 introduced the Stream API, which allows powerful operations like sorting using custom comparators. Java Comparator A Comparator is a functional interface in Java that defines custom sorting logic. It compares two objects and returns a result based on the comparison. Java Stream A Stream is a sequence of elements that can be processed in parallel or sequentially, supporting methods like sorting, filtering, and mapping. Sorting string stream with a reversed comparator The following are the steps for sorting a ... Read More

Sort String Array alphabetically by the initial character only in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:26

2K+ Views

Here, we are sorting string array alphabetically by the initial character i.e. ‘J’ for ‘John’ will come after ‘Chris’ since the first character of ‘Chris’ is ‘C’.Let us first create a String array:String[] strArr = { "PQRS", "AB", "RSTUVW", "RST", "U", "UVWXY", "OUJBG" };Now, sort the string array based on the first character:Arrays.sort(strArr, (str1, str2) -> str1.charAt(0) - str2.charAt(0));The following is an example to sort String Array alphabetically by the initial character only:Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       String[] strArr = { "PQRS", "AB", "RSTUVW", "RST", "U", "UVWXY", "OUJBG" }; ... Read More

Advertisements