Getting a TreeNode on a JTree

How can I find a specific node on a JTree, having the node's user object? Is there a way to do that without searching the tree with a loop?
[147 byte] By [Valerianoa] at [2007-11-27 9:33:59]
# 1

Using the JTree method, addSelectionPath, will select the specified node. For example:

DefaultMutableTreeNode nodeOne = new DefaultMutableTreeNode("Node One");

tree.addSelectionPath(new TreePath(nodeOne.getPath()));

paternostroa at 2007-7-12 22:57:00 > top of Java-index,Java Essentials,New To Java...
# 2

That's not really what I need.

Considering that I have a tree where every node has on its userObject, a String, I need something like:

DefaultMutableTreeNode node = tree.getNode("ABC");

I know the method getNode(Object userObject) doesn't exist, but what I'd like to know is if is there any other method that can do that for me...

I could search the entire tree looking on each node's userObject to know if it is the node I want do deal with, but that would be veeeery bad.

Valerianoa at 2007-7-12 22:57:00 > top of Java-index,Java Essentials,New To Java...
# 3

You could use the getNextMatch method of the Tree class. It's not exactly what you're looking for but using it in concert with the TreePath getLastComponent method it can return a TreeNode. You will need to provide a unique name when using the getNextMatch method as it returns a tree element mathcing the specified prefix name. See attached sample working code:

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.text.*;

import javax.swing.tree.*;

public class FindTreeNode

{

public static void main(String args[])

{

new FindTreeNodeFrame();

}

}

class FindTreeNodeFrame extends JFrame

{

private Vector nodes = new Vector();

private JTree tree;

private JPanel south = new JPanel();

private JTextField nodeName = new JTextField(20);

private JButton find = new JButton("Find");

public FindTreeNodeFrame()

{

super();

find.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

String name = nodeName.getText();

if (name != null && !name.trim().equals(""))

{

TreePath path = tree.getNextMatch(nodeName.getText(),0,Position.Bias.Forward);

if (path != null)

{

TreeNode treeNode = (TreeNode) path.getLastPathComponent();

System.out.println(treeNode);

}

}

}

});

south.add(new JLabel("Node Name:"));

south.add(nodeName);

south.add(find);

/* Add tree nodes */

nodes.add("Node One");

nodes.add("Node Two");

nodes.add("Node Three");

tree = new JTree(nodes);

/* Components should be added to the container's content pane */

Container cp = getContentPane();

cp.add(BorderLayout.NORTH,tree);

cp.add(BorderLayout.SOUTH,south);

/* Add the window listener */

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent evt)

{

dispose();

System.exit(0);

}

});

/* Size the frame */

pack();

setResizable(false);

/* Center the frame */

Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();

Rectangle frameDim = getBounds();

setLocation((screenDim.width - frameDim.width) / 2,(screenDim.height - frameDim.height) / 2);

/* Show the frame */

setVisible(true);

}

}

paternostroa at 2007-7-12 22:57:00 > top of Java-index,Java Essentials,New To Java...