• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTree closing all nodes

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic