- 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
How to find a node in a JTree Component with Java?
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 { 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); node.add(node2); node.add(node3); DefaultMutableTreeNode one = new DefaultMutableTreeNode("Learning website"); DefaultMutableTreeNode two = new DefaultMutableTreeNode("Business website"); DefaultMutableTreeNode three = new DefaultMutableTreeNode("News publishing website"); DefaultMutableTreeNode four = new DefaultMutableTreeNode("Android app"); DefaultMutableTreeNode five = new DefaultMutableTreeNode("iOS app"); DefaultMutableTreeNode six = new DefaultMutableTreeNode("Editor WebApp"); node1.add(one); node1.add(two); node1.add(three); node2.add(four); node2.add(five); node3.add(six); JTree tree = new JTree(node); int begnRow = 0; String prefix = "A"; TreePath treePath = tree.getNextMatch(prefix, begnRow, Position.Bias.Forward); System.out.println("Node = "+treePath); tree.setRowHeight(25); frame.add(tree); frame.setSize(550,400); frame.setVisible(true); } }
Output
The output is as follows. On running, the node which is to be found is visible −
- Related Articles
- How to get the leaf after this node in a JTree component 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
- How to allow only a single tree node to be selected in a JTree 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
- Can I get the node at a specified index in a JTree with Java?
- Java Program to get the previous node from a JTree
- Java Program to count the child of root node in a JTree
- Check whether a node is a root node or not in JTree
- How to get this nodes’s parent in a JTree with Java?
- How to get the number of siblings of this node in a JTree?
- How to make a JTree editable in Java?
- How to automatically resize a JTree in Java
- How to get this node’s last child in a JTree with Java?

Advertisements