aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTree class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree class" Watch "JTree class" New topic
Author

JTree class

sahitya
Greenhorn

Joined: May 01, 2008
Posts: 14
I have to retrieve the previous node using JTree class.

I can retrieve the currently selected node using getLastSelectedPathComponent() method

For eg:
TreeNode selArea = (TreeNode)layoutTree.getLastSelectedPathComponent();

Is there any method, that I can retrieve the previous node (last but one).

Your help is very much appreciated. Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Are you using TreeNodes in the JTree? If so, it is not that hard.

First of all, check if the node has a previous sibling:

If it isn't, you have to check the parent's sibling, and then choose the very last (grand)child of that:

Eventually you will find the last node because that will be a leaf.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sahitya
Greenhorn

Joined: May 01, 2008
Posts: 14
The way I explained is little bit confusing. I want to retrieve the previously selected node.

Suppose I have A1,A2,A3 and A4 node under A. First I have selected A1 and then I have selected A3. Now I need to get the previously selected node (i.e., A1)

I hope it is understood.

Sample code from my program.
private JTree layoutTree = null;
TreeNode selArea = (TreeNode)layoutTree.getLastSelectedPathComponent();
sahitya
Greenhorn

Joined: May 01, 2008
Posts: 14
The way I explained earlier is little bit confusing. Please find the detailed explanation of my problem.

Suppose I have A1,A2,A3 and A4 nodes under A. First I have selected A1 and then I have selected A3 (currently selected). Now I need to get the previously selected node (i.e., A1)

I hope it is understood.

Sample code from my program.
JTree layoutTree = null;
TreeNode layoutNodes = new TreeNode("default");
layoutTree = new JTree(layoutNodes)
TreeNode selArea = (TreeNode)layoutTree.getLastSelectedPathComponent();
//Will retrieve currently selected node

Now I need to retrieve the previously selected node (which may not be previous node).

Please help me. Let me know for any further clarifications.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You're just going to have to store that yourself. There isn't anything in the API holding this information for you.


GenRocket - Experts at Building Test Data
sahitya
Greenhorn

Joined: May 01, 2008
Posts: 14
How can I do that??
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Use a TreeSelectionListener. Check out when all of its events are triggered, and what information they contain.
 
 
subject: JTree class