- 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
Can we prevent the collapse of nodes and child nodes in a JTree with Java?n
Yes, we can prevent the collapse of nodes and child nodes in a JTree using setExpandedState() method. This method sets the expanded state of this JTree.If state istrue, all parents of path and path are marked asexpanded.
The following is an example that prevents the collapse of nodes and child nodes in a JTree with Java −
Example
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) { protected void setExpandedState(TreePath path, boolean state) { if (state) { super.setExpandedState(path, state); } } }; for (int i = 0; i < tree.getRowCount(); i++) { tree.expandRow(i); } tree.putClientProperty("JTree.lineStyle", "Angled"); 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
- Related Articles
- How to expand JTree row to display all the nodes and child nodes in Java
- Java Program to get the count of child nodes of any node in JTree
- How to get the leaves of nodes in a JTree with Java?
- How to check if two nodes are equal in a JTree with Java?
- How to allow contigous selection of nodes in a JTree?
- Locating child nodes of WebElements in selenium.
- Allow multiple selection of nodes not necessarily contigous in JTree
- How can we disable the leaf of JTree in Java?
- How to get this node’s last child in a JTree with Java?
- How to get this node’s first child in a JTree with Java?
- How to remove all child nodes from a parent in jQuery?
- Java Program to count the child of root node in a JTree
- Program to remove all nodes with only one child from a binary tree in Python?
- How to remove all child nodes from a parent using jQuery?
- How to remove all child nodes from a parent node using jQuery?

Advertisements