Found 7442 Articles for Java

How to create Glue to fill the space between neighbouring components in Java?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

585 Views

Let’s say we have 6 components and we need to fill the space between some of them −JButton button1 = new JButton("CSK"); JButton button2 = new JButton("DC"); JButton button3 = new JButton("MI"); JButton button4 = new JButton("SRH"); JButton button5 = new JButton("RR"); JButton button6 = new JButton("KKR");To fill the space and separate components, create a Glue using the createGlue() method −Box box = new Box(BoxLayout.X_AXIS); box.add(button1); box.add(button2); box.add(Box.createGlue()); box.add(button3); box.add(button4); box.add(Box.createGlue()); box.add(button5); box.add(button6);The following is an example to fill the space between neighbouring components −Examplepackage my; import java.awt.BorderLayout; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class ... Read More

Java program to get the count of child nodes of any node in JTree

Anvi Jain
Updated on 10-Oct-2024 11:38:52

858 Views

In this program, we will create a tree structure using JTree and DefaultMutableTreeNode in Java to represent a hierarchical structure, such as a website. We will then use the getChildCount() method to retrieve and display the number of child nodes for specific nodes in the tree. The example focuses on nodes that are not root nodes and prints the count of child nodes for each. Let’s say we want the count of child nodes for node 1, which is not a root node − node1.getChildCount() Steps to get the count of child nodes of any node in JTree Following are ... Read More

How to create a Box Layout in Java?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

3K+ Views

To create a Box Layout in Java Swing, use the BoxLayout class. Here, we have also set that the components should be laid our left to right or top to bottom −Box box = new Box(BoxLayout.X_AXIS); box.add(button1); box.add(button2); box.add(button3); box.add(button4); box.add(Box.createGlue()); box.add(button5); box.add(button6); box.add(button7); box.add(button8);Above, we have 8 buttons in our Box Layout. We have separated 4 buttons each using the createGlue() method.The following is an example to create a BoxLayout in Java −Examplepackage my; import java.awt.BorderLayout; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {     ... Read More

How to separate Components in a Row or Column with Box in Java

George John
Updated on 30-Jul-2019 22:30:26

304 Views

To separate components in a row or column, use the createGlue() method. This creates an invisible "glue" component that separates components.The following is an example to separate components in a row or column with Box −Examplepackage my; import java.awt.BorderLayout; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Matches");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JButton button1 = new JButton("CSK");       JButton button2 = new JButton("DC");       JButton button3 = new JButton("MI");       JButton ... Read More

Java program to determine when a Frame or Window is closing

Chandu yadav
Updated on 30-Aug-2024 11:43:36

2K+ Views

In this article, we will learn to determine when a Frame or Window is closing in Java. We will create a simple window in Java Swing titled "Demo". It displays styled text (italic, black on orange) using a text pane inside a scrollable area. The program also listens for window-closing events, printing a message when you close it. This basic example is how to build a GUI with text styling and event handling.Steps to determine when a Frame or Window is closing Following are the steps to determine when a Frame or Window is closing in Java − ... Read More

Determine when a Frame or Window is Opened in Java

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

926 Views

To determine when a Window is opened in Java, use the WindowListener, which is the listener interface for receiving window events..WindowListener listener = new WindowAdapter() {    public void windowOpened(WindowEvent evt) {       Frame frame = (Frame) evt.getSource();       System.out.println("Opened "+frame.getTitle());    } };Above, we have used the windowOpened() method, which is invoked when a window has been opened −public void windowOpened(WindowEvent evt) {    Frame frame = (Frame) evt.getSource();    System.out.println("Opened "+frame.getTitle()); }The following is an example to determine when a frame or window is opened in Java −Examplepackage my; import java.awt.BorderLayout; import java.awt.Color; import ... Read More

How to set the maximized bounds for a Frame in Java?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

484 Views

To set maximized bounds, use the setMaximizedBounds() method. Here, we will create a frame, which when maximized will form a shape −JFrame frame = new JFrame("Login!");Above, we have created a frame and now we will use the Rectangle class to specify an area of coordinates −Rectangle bounds = new Rectangle(50, 10, 100, 200); Now, set maximized bounds − frame.setMaximizedBounds(bounds);The following is an example to set the maximized bounds for a frame −Examplepackage my; import java.awt.GridLayout; import java.awt.Rectangle; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception { ... Read More

How to disable close button on a JFrame in Java?

George John
Updated on 30-Jul-2019 22:30:26

990 Views

To disable the close button, let us first create a frame −JFrame frame = new JFrame("Login!");Use the setDefaultCloseOperation() to set the status of the close button. Set it to DO_NOTHING_ON_CLOSE to disable it −frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);The following is an example to disable close button on a JFrame in Java −Examplepackage my; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Login!");       JLabel label1, label2, label3;       frame.setLayout(new GridLayout(2, 2));       label1 = ... Read More

How can I position JButtons vertically one after another in Java Swing?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

634 Views

Position buttons vertically one after another using the Box class. Use the createVerticalBox() method that displays components from top to bottom −JButton button1 = new JButton("One"); JButton button2 = new JButton("Two"); JButton button3 = new JButton("Three"); Box box = Box.createVerticalBox(); box.add(button1); box.add(button2); box.add(button3);The following is an example to position buttons vertically one after another −Examplepackage my; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JButton button1 = new JButton("One");       JButton button2 = new JButton("Two");       JButton button3 = new JButton("Three");       ... Read More

How to set the text of the JLabel to be right-justified and vertically centered in Java?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

868 Views

To set the text of the label component to be right- justified and vertically centered, you need to set the alignment while creating a new label.Set the label to be on the right -JLabel label = new JLabel("Name", JLabel.RIGHT);Here, we have set the size of the label as well as the color that includes foreground and background color -label.setPreferredSize(new Dimension(170, 70)); label.setOpaque(true); label.setBackground(Color.MAGENTA); label.setForeground(Color.WHITE);The following is an example to set the text of the label to be right-justified and vertically centered −Examplepackage my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public ... Read More

Advertisements