
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 9150 Articles for Object Oriented Programming

927 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

340 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

6K+ 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

181 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

441 Views
To get the depth of root node in a JTree in Java, use the getDepth() method. Let’s say our root node is “node”, therefore, we will get the depth of root node like this −node.getDepth();The following is an example to get the depth of root node −Examplepackage 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 DefaultMutableTreeNode("Tutorials"); DefaultMutableTreeNode ... Read More

8K+ Views
To set location of a component in 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 −JLabel label = new JLabel("Demo Label!"); Dimension size = label.getPreferredSize(); label.setBounds(150, 100, size.width, size.height);The following is an example to set location of a label in a JFrame −Examplepackage my; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.WindowConstants; public ... Read More

848 Views
To find a node in a JTree component, use the getNextMatch() method. Here, wer are trying to find a node that begins with character “A”. The search begins from the node set below with begnRow variable −int begnRow = 0; String prefix = "A"; TreePath treePath = tree.getNextMatch(prefix, begnRow, Position.Bias.Forward);We have displayed the resultant node in the Console −System.out.println("Node = "+treePath);The following is an example to find a node in a JTree Component with Java −package my; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.text.Position; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; public class SwingDemo { public static void main(String[] args) throws Exception { ... Read More

481 Views
To set the row height of a JTree, use the setRowHeight() and as a parameter set the height in pixels −tree.setRowHeight(25);The following is an example to set the row height of a JTree with Java −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("Project"); DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("App"); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Website"); DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("WebApp"); node.add(node1); ... Read More

651 Views
To set FlowLayout for a frame, use the Container. At first, set a JFrame −JFrame frame = new JFrame();Now, use Container and set the layout as FlowLayout−Container container container = frame.getContentPane(); container.setLayout(new FlowLayout());The following is an example to set FlowLayout for JFrame −Examplepackage my; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; public class SwingDemo { public static void main(String[] val) { JFrame frame = new JFrame(); Container container; JButton btn; frame.setBounds(20, 20, 15, 15); container = frame.getContentPane(); container.setLayout(new ... Read More

9K+ Views
Let us create DefaultTableModel −DefaultTableModel tableModel = new DefaultTableModel();Now, set the model to JTable −JTable table = new JTable(tableModel);Add a column −tableModel.addColumn("Languages");Now, we will add rows to our table −tableModel.insertRow(0, new Object[] { "CSS" }); tableModel.insertRow(0, new Object[] { "HTML5" });The following is an example to create DefaultTableModel −Examplepackage my; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class SwingDemo { public static void main(String[] argv) throws Exception { DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel); tableModel.addColumn("Languages"); tableModel.insertRow(0, new Object[] { "CSS" }); ... Read More