How to know if a TreePath is part of a JTree

Hello.

I need to figure out if a given TreePath belongs to a given JTree. However, I haven't found any method for finding this out, nor have a found a method that returns all the TreePaths belonging to a JTree. I've only found "getExpandedDescendants" which only returns the expanded TreePaths. It seems kind of weird that you can get a subgroup of the TreePaths but now the whole group...

Thank you for any help.

[434 byte] By [Kryigerofa] at [2007-10-3 11:59:52]
# 1
I am not sure what you are asking but one method that might work:getClosestPathForLocation(int x, int y) I guess I am confused with what you are trying to do?
zadoka at 2007-7-15 14:36:08 > top of Java-index,Desktop,Core GUI APIs...
# 2

If I am understanding your question correctly. In order to return a given tree path you have to have some type of event processes on that tree. In the example you have given, it looks as if you get the tree path if the node is expanded!? is this correct.

You can get different tree path event based upon the event you listen for. So, for example it may be possible to get a TreePath on a mouse click if you click on the node. Or make even a mouse over.

The point is - I believe you have to either run an event on a node, or maybe do a comparison.

If you do the comparison. You would have to get the tree path infomation ahead of time, and do comparison on the fly, which will probably be another event.

orozcoma at 2007-7-15 14:36:08 > top of Java-index,Desktop,Core GUI APIs...
# 3

I'll try to clarify (unfortunately I don't have the code handy right now, I'll post it next Monday if needed):

I have JTree "theJTree" and TreePath "theTreePath". I want to do something like this:

if (theJTree.contains(theTreePath)){

//do something

}

But theres no JTree.contains(TreePath) method.

If I were only interested in the expanded TreePaths in the JTree, I could do:

if (theJTree.getExpandedDescentants(theTree.getRoot()).contains(theTreePath)){

//do something

}

But I'm also interested in those TreePaths within the JTree that aren't expanded.

Using special events to this trivial seeming thing sounds like an overkill.

Message was edited by:

Kryigerof

Kryigerofa at 2007-7-15 14:36:08 > top of Java-index,Desktop,Core GUI APIs...
# 4

> But theres no JTree.contains(TreePath) method

actuall there is, but you may have to modify it a little.

I am working on something similar, and this is what I have:

in my valueChanged method I have this.

JTree treeSource = (JTree) treeSelectionEvent.getSource();

TreePath path = treeSource.getSelectionPath();

Here is a running example. Just compile and run. It will display your info the console.

import java.awt.BorderLayout;

import java.util.Enumeration;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTree;

import javax.swing.event.TreeSelectionEvent;

import javax.swing.event.TreeSelectionListener;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.TreeModel;

import javax.swing.tree.TreeNode;

import javax.swing.tree.TreePath;

public class MainClass_1

{

public static void main(String args[])

{

JFrame frame = new JFrame("Traverse Tree");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

///////////////////////////////////////////

// Test JTree From Here

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Device");

DefaultMutableTreeNode module = new DefaultMutableTreeNode("Module");

root.add(module);

DefaultMutableTreeNode status = new DefaultMutableTreeNode("Status");

DefaultMutableTreeNode stNetwork = new DefaultMutableTreeNode("Network");

DefaultMutableTreeNode stDevice = new DefaultMutableTreeNode("Device");

DefaultMutableTreeNode stChassis = new DefaultMutableTreeNode("Chassis");

DefaultMutableTreeNode stResources = new DefaultMutableTreeNode("Resources");

status.add(stNetwork);

status.add(stDevice);

status.add(stChassis);

status.add(stResources);

DefaultMutableTreeNode projEdit = new DefaultMutableTreeNode("Project Editor");

DefaultMutableTreeNode projEditPage = new DefaultMutableTreeNode("Project Editor Page");

projEdit.add(projEditPage);

DefaultMutableTreeNode projMngt = new DefaultMutableTreeNode("Project Management");

DefaultMutableTreeNode projMngtPage = new DefaultMutableTreeNode("Project Management Page");

projMngt.add(projMngtPage);

DefaultMutableTreeNode admin = new DefaultMutableTreeNode("Administration");

DefaultMutableTreeNode admNetwork = new DefaultMutableTreeNode("Network");

DefaultMutableTreeNode admDevice = new DefaultMutableTreeNode("Device");

DefaultMutableTreeNode admUsers = new DefaultMutableTreeNode("Users");

admin.add(admNetwork);

admin.add(admDevice);

admin.add(admUsers);

DefaultMutableTreeNode logging = new DefaultMutableTreeNode("Logging");

DefaultMutableTreeNode loggingPage = new DefaultMutableTreeNode("Logging Page");

logging.add(loggingPage);

module.add(status);

module.add(projEdit);

module.add(projMngt);

module.add(admin);

module.add(logging);

JTree tree = new JTree(root);

// End Test Here

/////////////////////////////////////////

//JTree tree = new JTree();

tree.setRootVisible(true);

TreeModel model = tree.getModel();

//System.out.println("Model: " + model);

Object rootObject = model.getRoot();

if ((rootObject != null) &&

(rootObject instanceof DefaultMutableTreeNode))

{

DefaultMutableTreeNode r = (DefaultMutableTreeNode) rootObject;

printDescendents(r);

Enumeration breadth = r.breadthFirstEnumeration();

Enumeration depth = r.depthFirstEnumeration();

Enumeration preOrder = r.preorderEnumeration();

printEnumeration(breadth, "Breadth");

printEnumeration(depth, "Depth");

printEnumeration(preOrder, "Pre");

}

TreeSelectionListener treeSelectionListener =

new TreeSelectionListener()

{

public void valueChanged(TreeSelectionEvent treeSelectionEvent)

{

JTree treeSource = (JTree) treeSelectionEvent.getSource();

TreePath path = treeSource.getSelectionPath();

System.out.println("Path: " + path);

System.out.println("path.getPath() method: " + path.getPath());

System.out.println("path.getParentPath() method: " + path.getParentPath());

System.out.println(((DefaultMutableTreeNode)

path.getLastPathComponent()).getUserObject());

System.out.println("path.getPathCount() method: " + path.getPathCount());

}

};

tree.addTreeSelectionListener(treeSelectionListener);

JScrollPane scrollPane = new JScrollPane(tree);

frame.add(scrollPane, BorderLayout.CENTER);

frame.setSize(300, 400);

frame.setVisible(true);

}

private static void printEnumeration(Enumeration e, String label)

{

System.out.println("--" + label + "--");

while (e.hasMoreElements())

{

System.out.println(e.nextElement());

}

}

public static void printDescendents(TreeNode root)

{

System.out.println(root);

Enumeration children = root.children();

if (children != null)

{

while (children.hasMoreElements())

{

printDescendents((TreeNode) children.nextElement());

}

}

}

}

I know the code looks terrible, but this is just for testing. I am going to be putting in a getTree method. and all sorts of other goodies. I hope this helps.

orozcom

I hope this helps

orozcoma at 2007-7-15 14:36:08 > top of Java-index,Desktop,Core GUI APIs...