Found 4338 Articles for Java 8

How to get the depth of root node in a JTree with Java?

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

269 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

How to set location of JLabel in a JFrame with Java?

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

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

How to find a node in a JTree Component with Java?

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

592 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

How to set the Row Height of a JTree with Java?

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

332 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

How to set FlowLayout for JFrame in Java?

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

527 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

How to create DefaultTableModel which is an implementation of TableModel

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

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

How to add a tooltip to a component in Java?

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

370 Views

Let us first create a component −JLabel label1; label1 = new JLabel("Id", SwingConstants.CENTER); Now, set the tooltip for the above component − label1.setToolTipText("Enter Id");ExampleThe following is an example to add a tooltip to a component in Java −package my; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.Point; 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.setDefaultLookAndFeelDecorated(true);       JFrame frame = new JFrame("Register!");       JLabel label1, label2, label3;       frame.setLayout(new GridLayout(2, 2));       label1 = new JLabel("Id", ... Read More

How to get this node’s last child in a JTree with Java?

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

199 Views

Let’s say we want the last child node of a node, then use the getLastChild() method −node3.getFirstChild()Display the node’s last child on Console −System.out.println("The last child of node 3 = "+node3.getLastChild());The following is an example to get this node’s last child in a JTree −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("Products");       DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing (Product1 - P66778)");       DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Accessories ... Read More

How to add separator in a ToolBar with Java?

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

125 Views

To add a separator, use the addSeparator() method. Let us first create a toolbar −JToolBar toolbar = new JToolBar();Now, we will create some components and set a separator to separate them as shown below −toolbar.add(new JTextArea(" Add name here")); toolbar.add(new JButton("Submit Name")); toolbar.addSeparator(); toolbar.add(new JTextArea(" Add age here")); toolbar.add(new JButton("Submit Age"));The following is an example to add separator in a ToolBar with Java −Examplepackage my; import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JToolBar; public class SwingDemo {    public static void main(String[] args) {       JPanel panel = new JPanel(new BorderLayout());       ... Read More

Java Program to display JTabbedPane on the right

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

83 Views

To display JTabbedPane on the right, you need to set the location to the right using the constant RIGHT −JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT);The following is an example to display JTabbedPane on the right −Examplepackage my; import javax.swing.*; public class SwingDemo {    public static void main(String args[]) {       JFrame frame = new JFrame("Technologies");       JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT);       JPanel panel1, panel2, panel3, panel4, panel5;       panel1 = new JPanel();       panel2 = new JPanel();       panel3 = new JPanel();       panel4 = ... Read More

Advertisements