- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to get this node’s first child in a JTree with Java?
Let’s say we want the first child node of a node, then use the getFirstChild() method −
node2.getFirstChild()
Display the node’s first child on Console −
System.out.println("The first child of node 2 = "+node2.getFirstChild());
The following is an example to get this node’s first child 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 { JFrame frame = new JFrame("Demo"); DefaultMutableTreeNode node = new DefaultMutableTreeNode("Products"); DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Clothing (Product1 - P66778)"); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Accessories (Product2 - P66779)"); DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("Home Decor (Product3 - P66780)"); DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Electronics (Product4 - P66781)"); node.add(node1); node.add(node2); node.add(node3); node.add(node4); DefaultMutableTreeNode one = new DefaultMutableTreeNode("Shirt"); DefaultMutableTreeNode two = new DefaultMutableTreeNode("T-shirt"); DefaultMutableTreeNode three = new DefaultMutableTreeNode("Hoodie"); DefaultMutableTreeNode four = new DefaultMutableTreeNode("Sunglasses"); DefaultMutableTreeNode five = new DefaultMutableTreeNode("Frames"); DefaultMutableTreeNode six = new DefaultMutableTreeNode("Belts"); DefaultMutableTreeNode seven = new DefaultMutableTreeNode("Showpieces & Figurines"); DefaultMutableTreeNode eight = new DefaultMutableTreeNode("Clocks"); DefaultMutableTreeNode nine = new DefaultMutableTreeNode("Wall Shelves"); DefaultMutableTreeNode ten = new DefaultMutableTreeNode("Mobile Accessories"); DefaultMutableTreeNode eleven = new DefaultMutableTreeNode("Smart Wearable Tech"); DefaultMutableTreeNode twelve = new DefaultMutableTreeNode("Health Care Applicances"); DefaultMutableTreeNode thirteen = new DefaultMutableTreeNode("Smart Home Automation"); node1.add(one); node1.add(two); node1.add(three); node2.add(four); node2.add(five); node2.add(six); node3.add(seven); node3.add(eight); node3.add(nine); node4.add(ten); node4.add(eleven); node4.add(twelve); node4.add(thirteen); JTree tree = new JTree(node); for (int i = 0; i < tree.getRowCount(); i++) { tree.expandRow(i); } tree.putClientProperty("JTree.lineStyle", "Angled"); System.out.println("Node 2 = "+node.getChildAt(1)); System.out.println("The first child of node 2 = "+node2.getFirstChild()); tree.setRowHeight(25); frame.add(tree); frame.setSize(600,450); frame.setVisible(true); } }
The output is as follows dislaying this node’s first child on Console −
The following is our JTree −
- Related Articles
- How to get this node’s last child in a JTree with Java?
- How to get the leaf after this node in a JTree component with Java?
- Java Program to get the count of child nodes of any node in JTree
- How to get this nodes’s parent in a JTree with Java?
- How to get the depth of root node in a JTree with Java?
- I want to return the next node after this node in a JTree with Java
- Java Program to count the child of root node in a JTree
- How to get the number of siblings of this node in a JTree?
- How to find a node in a JTree Component with Java?
- Get the number of levels above a node in JTree with Java
- Get the number of siblings of a node in JTree with Java
- Java Program to get the previous node from a JTree
- Can I get the node at a specified index in a JTree with Java?
- How to allow only a single tree node to be selected in a JTree with Java?
- First and last child node of a specific node in JavaScript?

Advertisements