Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching in JTree

Status
Not open for further replies.

SValmont2000

Technical User
Nov 26, 2001
5
CA
Hey @ll,

I have a problem and am somewhat stuck. I have a JTree, with the DefaultTreeModel. I have a DefaultMutableTreeNode where othere Def.Mut.TN are attached. How can I get a specific TreeNode, e.g. in my TreeNodes are always Strings stored, so how can I get the TreeNode with an specific String.

Thanks for the help.

S. V.
 
The DefaultMutableTreeNode class defines two public methods which may be of use to you:
Code:
public java.util.Enumeration breadthFirstEnumeration();
public java.util.Enumeration depthFirstEnumeration();
So I guess you can find the node you want with code similar to:
Code:
public DefaultMutableTreeNode findNode( DefaultMutableTreeNode root, String search ) {

    Enumeration nodeEnumeration =
        root.breadthFirstEnumeration();
    while( nodeEnumeration.hasMoreElements() ) {
        DefaultMutableTreeNode node =
            (DefaultMutableTreeNode)nodeEnumeration.nextElement();
        String found = (String)node.getUserObject();
        if( search.equals( found ) ) {
            return node;
        }
    }
    return null;
}
Note, I haven't actually tested this code :) Hope it works for you... Cheers, Neil
 
A more complete code example.
Code:
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.Enumeration;

public class TestTree extends JFrame {

  private JTree jTree;
  DefaultTreeModel defaultTreeModel;

  public TestTree() {
    super( "Test Tree" );
    setSize( 600, 450 );
  }

  public void buildTree( DefaultMutableTreeNode root ) {
    // tree to look like:
    // root
    //  a -> b c
    //  A -> B C
    DefaultMutableTreeNode a = new DefaultMutableTreeNode( "a" );
    DefaultMutableTreeNode b = new DefaultMutableTreeNode( "b" );
    DefaultMutableTreeNode c = new DefaultMutableTreeNode( "c" );

    DefaultMutableTreeNode A = new DefaultMutableTreeNode( "A" );
    DefaultMutableTreeNode B = new DefaultMutableTreeNode( "B" );
    DefaultMutableTreeNode C = new DefaultMutableTreeNode( "C" );

    a.add( b );
    a.add( c );
    root.add( a );

    A.add( B );
    A.add( C );
    root.add( A );
  }

  public void init( String search )  {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode( "root" );
    buildTree( root );
    defaultTreeModel = new DefaultTreeModel( root );
    jTree = new JTree( defaultTreeModel );
    getContentPane().add( new JScrollPane( jTree ), BorderLayout.CENTER );
    DefaultMutableTreeNode foundNode = findNode( root, search );
    TreePath path = new TreePath( foundNode.getPath() );
    System.out.println( "path: " + path.toString() );
    jTree.setEditable( true );
    jTree.makeVisible( path );
    jTree.setSelectionPath( path );
  }

  public DefaultMutableTreeNode findNode( DefaultMutableTreeNode root, String search ) {
    Enumeration nodeEnumeration = root.breadthFirstEnumeration();
    while( nodeEnumeration.hasMoreElements() ) {
      DefaultMutableTreeNode node =
        (DefaultMutableTreeNode)nodeEnumeration.nextElement();
      String found = (String)node.getUserObject();
      if( search.equals( found ) ) {
        return node;
      }
    }
    return null;
  }

  public static void main( String[] args ) {
    try {
      if( args.length != 1 ) {
        System.out.println(
          "Usage: java TestTree " +
          "[search for node]" );
        System.exit(0);
      }
      TestTree testTree = new TestTree();
      testTree.init( args[0] );
      testTree.setVisible( true );
    } catch( Exception e ) {
      e.printStackTrace();
    }
  }
}
Cheers, Neil :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top