-
JTree & iterating through all selected Nodes
Hi all,
I'm looking for a means to identify all selected nodes within a JTree so that I can extract the object that I used to build each node. I have the JTree set-up and displaying properly. Single node selection works, but the program gets "confused" when multiple nodes are selected. It will properly extract the first and second node, and then all subsequent nodes are the same as the second node.
What am I doing wrong?
TreePath[] nodes = tree.getSelectionPaths();
int[] dwgs = new int[nodes.length];
for(int i = 0; i < nodes.length ; i++)
{
TreePath temp = nodes[i];
Object tempObj = tree.getLastSelectedPathComponent();
DefaultMutableTreeNode treNode = (DefaultMutableTreeNode)tempObj;
Drawing dwg = (Drawing)treNode.getUserObject();
System.out.println("Found Dwg ID #" + dwg.idNumber()); // bit of debugging here
dwgs[i] = dwg.idNumber();
tree.removeSelectionPath(temp);
}
I don't want to use the TreeSelectionListener interface as I already have this doing other work in the program (displaying additional information on node selection).
Any help would be appreaciated.
James D.
-
TreePath temp = nodes[i];
// here you get one of the selected nodes (actually the path to it)
Object tempObj = tree.getLastSelectedPathComponent();
// but you never use that node in the loop, except at the end to deselect it.
// I would replace this line of code by:
Object tempObj = temp.getLastPathComponent()
-
That's the ticket!! Thanks for your help.
(I've really got to get my work to get me a proper IDE instead of doing this in notepad!!!)
Thanks again.
James D.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks