• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTree closing all nodes  RSS feed

 
Sven Goetgeluck
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
i like to close all nodes from tree except the selected one,
so when the selection of the tree changes the nodes exand or collaps,

anyone has tips?

wth rgds
Sven
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sven,

You can create a method that will iterate over all the visible rows in the JTree and collapse them. You can use the methods getRowCount() and collapseRow() to do this.
Then simply expand the previously selected path. You can use getLeadSelectionPath() and expandPath() here.

It might be necessary to store the selected path before collapsing all the rows, you'll have to see for yourself. Depending on your taste, you can also choose to collapse the children of the selected path after you've done all this.

Finally, add a TreeSelectionListener to the JTree that calls this method.

Warning, the value of getRowCount() may change as you make calls to collapseRow(), so make sure to keep checking it. Either that, or iterate the tree in reverse order.

 
Sven Goetgeluck
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
txs,

i'll go for the reverse method, that the way a save my tree,
so maybe with small modifications it would word

s.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!