- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4338 Articles for Java 8
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
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
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
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
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
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
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
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
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
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
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
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
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
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
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
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
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
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
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
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