Java JTree get tree path from string path

Description

Java JTree get tree path from string path

import java.awt.Container;
import java.awt.FlowLayout;
import java.util.StringTokenizer;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class Main {
   public static void main(String args[]) {
      final JFrame f = new JFrame("JTree Demo");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container c = f.getContentPane();
      c.setLayout(new FlowLayout());
      DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
      DefaultMutableTreeNode c1 = new DefaultMutableTreeNode("A");
      DefaultMutableTreeNode c2 = new DefaultMutableTreeNode("B");
      DefaultMutableTreeNode c3 = new DefaultMutableTreeNode("C");
      root.add(c1);/*from ww w.  j  ava 2 s  .  com*/
      root.add(c2);
      root.add(c3);

      c1.add(new DefaultMutableTreeNode("1"));
      c1.add(new DefaultMutableTreeNode("2"));

      c2.add(new DefaultMutableTreeNode("3"));
      c2.add(new DefaultMutableTreeNode("4"));

      c3.add(new DefaultMutableTreeNode("5"));
      DefaultMutableTreeNode n6 = new DefaultMutableTreeNode("6");

      c3.add(n6);

      JTree t = new JTree(root);

      TreePath path = getTreePath(t.getModel(),"Root,A,1",",");
      System.out.println(path);

      c.add(new JScrollPane(t));
      f.pack();
      f.setVisible(true);
   }

   public static TreePath getTreePath(TreeModel treeModel, String pathString, String pathSeparator) {
      if (treeModel == null) {
         throw new NullPointerException("treeModel == null");
      }
      if (pathString == null) {
         throw new NullPointerException("pathString == null");
      }
      if (pathSeparator == null) {
         throw new NullPointerException("pathSeparator == null");
      }
      StringTokenizer tokenizer = new StringTokenizer(pathString, pathSeparator);
      int tokenCount = tokenizer.countTokens();
      int tokenNumber = 1;
      int tokenFoundCount = 0;
      Object[] path = new Object[(tokenCount > 0) ? tokenCount : 1];
      if (tokenCount > 0) {
         path[0] = treeModel.getRoot();
         tokenizer.nextToken();
         Object currentElement = treeModel.getRoot();
         boolean appended = true;
         while (appended && (tokenNumber < tokenCount)) {
            int childCount = treeModel.getChildCount(currentElement);
            String pathToken = tokenizer.nextToken();
            boolean found = false;
            appended = false;
            for (int index = 0; (index < childCount) && !found; index++) {
               Object childElement = treeModel.getChild(currentElement, index);
               found = childElement.toString().equals(pathToken);
               if (found) {
                  path[tokenNumber] = childElement;
                  currentElement = childElement;
                  appended = true;
                  tokenFoundCount++;
               }
            }
            tokenNumber++;
         }
      }
      return ((tokenCount > 0) && (tokenCount - 1 == tokenFoundCount)) ? new TreePath(path) : null;
   }
}



PreviousNext

Related