How to expand JTree row to display all the nodes and child nodes in Java

Java 8Object Oriented ProgrammingProgramming

To expand JTree row to display all the nodes and child nodes, use the expandRow() method in Java. At first, create a node −

DefaultMutableTreeNode node = new DefaultMutableTreeNode("Project");

Now, add nodes to the node created above −

DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("App");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Website");
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("WebApp");
node.add(node1);
node.add(node2);
node.add(node3);

Now, create more nodes and set them as child nodes for the nodes we creted above −

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);

Now, create a JTree and set the root node in it

JTree tree = new JTree(node);

Use the expandRow() method and loop it through the count of rows −

for (int i = 0; i < tree.getRowCount(); i++) {
   tree.expandRow(i);
}

The following is an example to expand JTree row to display all the nodes and child nodes −

Example

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);
      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);
      for (int i = 0; i < tree.getRowCount(); i++) {
         tree.expandRow(i);
      }
      tree.putClientProperty("JTree.lineStyle", "Angled");
      frame.add(tree);
      frame.setSize(550,400);
      frame.setVisible(true);
      frame.setLocationRelativeTo(null);
   }
}

Output

raja
Published on 06-Jun-2019 08:45:39
Advertisements