Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTree class

 
sahitya
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sahitya
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're just going to have to store that yourself. There isn't anything in the API holding this information for you.
 
sahitya
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I do that??
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a TreeSelectionListener. Check out when all of its events are triggered, and what information they contain.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic