• Post Reply Bookmark Topic Watch Topic
  • New Topic

Expanding and selecting a TreePath from within a program  RSS feed

 
Rupert Brown
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at all the literature this should be simple - but I am struggling here.

I have a JTree in a JPanel - its basically the editable tree from the Sun Java tutorial. The JPanel I put inside another JPanel. The structure of the tree comes from an XML file that gets read in the constuctor of the JTree JPanel. All of that works very well.

The problem I have is that I want to make visible and select the item in the tree that was selected when the application was closed. To do this I save the TreePath of the last selected item and retrieve it when the application is reopened. I know that bit works as well. It's just the making visible and selecting of the TreePath that is giving me grief. I'm using code that is straightforward:


where tbAR.getLastActivityPath() is a TreePath.

Are there any strange rules about using the methods scrollPathToVisible and setSelectionPath? My JTree is within a JScrollPane.

Many thanks for any suggestions.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you try calling tree.setSelectionPath before tree.scrollPathToVisible?
 
Rupert Brown
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a good idea - I've tried it and still when my application opens up I don't get the highlighted path.

I've tried putting the code in two places - in the constructor of the class that creates the JTree JPanel - so in theory the JTree JPanel is created and then the setSelection and scrollTo Path methods are called. The second place was in a method called after all the constructors are finished. Again no luck.

I don't need to do clever things with DefaultTreeSelectionModel do I - or something like that?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, without looking at your code, it is difficult to tell. What you need to do is post sscce code, which illustrates your problem, which we can download and test in our environment.
 
Rupert Brown
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh,

Thanks for your input. I went back and had another look. I was making some basic incorrect assumptions about the nature of JTrees. Anyway, I found my answer at:

http://www.exampledepot.com/egs/javax.swing.tree/FindNode.html

Cheers, Rupert
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done and thank you for posting a link which helped you fix your problem. Future users will find it useful.
The Java API also provides sample code. It can be found at http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!