Found 4338 Articles for Java 8

Get the number of levels above a node in JTree with Java

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

245 Views

To get the number of levels above a node, use the getLevel() method. Following is an example for the root node “node” −node.getLevel()Note − The value 0 is returned if the node is a root node since there are zero levels above the root node.For other nodes, get the number of levels above a node as shown below with node3 −node3.getLevel()The following is an example to get the number of levels above a node in a JTree −package my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; public class SwingDemo {    public static void main(String[] args) throws Exception {     ... Read More

How to use GridBagConstraints to layout two components in the same line with Java

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

237 Views

To align two components in the same line, you need to set the contrainsts of the GridBagConstraints properly. Let’s say we have two components in panel1. Set the contraints using Insets as well −panel1.add(checkBox1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); panel1.add(checkBox2, new GridBagConstraints(1, 0, 1, 1, 2.0, 0.0,    GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));The following is an example to set two components in the same line −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {   ... Read More

How to set Palette Layer for JDesktopPane in Java?

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

137 Views

At first, create a JDesktopPane −JDesktopPane desktopPane = new JDesktopPane();Now, create an Internal Frame −JInternalFrame intFrame = new JInternalFrame("Our Frame", true, true, true, true); intFrame.setBounds(50, 90, 200, 250);Set Pallette layer for JDesktopPane and add the Internal Frame to JDesktopPane −intFrame.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE); desktopPane.add(intFrame, JDesktopPane.PALETTE_LAYER);The following is an example to set palette layer for JDesktopPane −package my; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(final String[] args) {       JFrame frame = new JFrame("Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JDesktopPane desktopPane = new JDesktopPane();   ... Read More

Display multiple components in an Internal Frame in Java

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

315 Views

At first, set an Internal Frame in the JDesktopPane −JDesktopPane desktopPane = new JDesktopPane(); JInternalFrame intFrame = new JInternalFrame("Our Frame", true, true, true, true); desktopPane.add(intFrame);Now, set the bounds of the Internal Frame −intFrame.setBounds(50, 90, 200, 250);Create two components −JLabel label = new JLabel(intFrame.getTitle(), JLabel.CENTER); JButton button = new JButton("Demo Button");Add the two components to the Internal Frame −intFrame.add(label, BorderLayout.CENTER); intFrame.add(button, BorderLayout.WEST);The following is an example to display multiple components in an internal frame −package my; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(final String[] args) {   ... Read More

How to position component at the top of the grid cell with GridBagLayout in Java?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

500 Views

To position components at the top fo the grid cell, set the GridBagConstraints. Let’s say we have a panel and within that two CheckBox components −JPanel panel1 = new JPanel(new GridBagLayout()); JCheckBox checkBox1 = new JCheckBox("Undergraduate"); JCheckBox checkBox2 = new JCheckBox("Graduate");Position the components now −panel1.add(checkBox1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,    GridBagConstraints.NORTH, GridBagConstraints.NORTH, new Insets(0, 0, 0, 0), 0, 0)); panel1.add(checkBox2, new GridBagConstraints(1, 0, 1, 1, 2.0, 0.0,    GridBagConstraints.NORTH, GridBagConstraints.NORTH, new Insets(0, 0, 0, 0), 0, 0));The following is an example to position component at the top of the grid cell with GridBagLayout −Examplepackage my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; ... Read More

How to add Internal Frame to a JDesktopPane in Java?

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

522 Views

At first, create a JDesktoPane −JDesktopPane desktopPane = new JDesktopPane();Now, create an Internal frame and add it to the JDesktopPane −JInternalFrame intFrame = new JInternalFrame("Our Frame", true, true, true, true); desktopPane.add(intFrame); The following is an example to add Internal Frame to a JDesktopPane − package my; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(final String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JDesktopPane desktopPane = new JDesktopPane();       JInternalFrame intFrame = new JInternalFrame("Our Frame", true, ... Read More

How to center a JLabel in a JPanel with GridBagLayout in Java?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

720 Views

Center a component in a JPanel with GridBagLayout. Let us first create a JFrame and JPanel inside it -JFrame frame = new JFrame("Demo Frame"); JPanel panel = new JPanel();Now, we will add our components −JLabel label = new JLabel("Demo Label (Centered)"); label.setForeground(Color.white); JCheckBox checkBox = new JCheckBox("Checkbox (Centered)");Set the layout −panel.setLayout(new GridBagLayout());The following is an example to center a label in a panel with GridBagLayout −Examplepackage my; import java.awt.Color; import java.awt.GridBagLayout; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.WindowConstants; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame("Demo ... Read More

How to create a LineBorder with BorderFactory class in Java?

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

178 Views

To create line border, use the createLineBorder() method. Let us first create a label component −JLabel label; label = new JLabel("Demo label");Now, create line border with BorderFactory class. Here, we have also set the color for the line border −label.setBorder(BorderFactory.createLineBorder(Color.yellow));The following is an example to create a LineBorder with BorderFactory class −package my; import javax.swing.BorderFactory; import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       JLabel label;       label = new JLabel("Demo label");       ... Read More

How to set the location of a button anywhere in JFrame?

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

4K+ Views

To set location of a button anywhere a JFrame, use the setBounds() method. Let us first create a frame and a panel −JFrame frame = new JFrame("Demo Frame"); JPanel panel = new JPanel(); frame.getContentPane();Create a component i.e. label and set the dimensions using setBounds(). Here, you can set the location in the form of x and y coordinates and place the button anywhere in a frame −JButton button = new JButton("Demo Button"); Dimension size = button.getPreferredSize(); button.setBounds(300, 180, size.width, size.height);The following is an example to set the location of a button anywhere in JFrame −Examplepackage my; import java.awt.Dimension; import javax.swing.BorderFactory; ... Read More

Get the total number of leaves of a JTree in Java?

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

95 Views

To get the total number of leaved of a JTree, apply the getLeafCount() method on the root node. Let’s say our root node is “node”, therefore to count the number of leaves, use −node.getLeafCount()The following is an example to get the total number of leaves of a JTree −package my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame("Demo");       DefaultMutableTreeNode node = new DefaultMutableTreeNode("Website");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Videos");       DefaultMutableTreeNode node2 = new ... Read More

Advertisements