• 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 expansion/collapse callback

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a need in an application to allow an end user two ways to expand a collapsed node that exists in a tree view (JTree). For example, one way could be to simply click within the + square, and the other could be to simply double click on the node name. I also need to know which mechanism of the two was used to request the expansion. As far as I can tell though, there is only one callback mechanism for when the node is requested to be expanded.
I only need a single way to collapse an expanded node, and JTree already fully supports my needs in that regard.
Any ideas?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it seems that JTree will already expand a node in the two cases you describe - single click on the + sign or double click on the node name. What it doesn't do is tell you which of those two methods was used. However the API for JTree does give sample code for detecting a single or double click on a node using a MouseListener, which may be enough for what you want. You may want to use the MouseListener in conjunction with a TreeSelectionListener. Perhaps the MouseListener should decide for itself if a given MouseEvent should cause a an expansion or collapse - this might give you the best control in deciding what should happen. In this event your MouseListener code should probably call e.consume() on the MouseEvent when it's done, so that the default processing of the MouseEvent doesn't countermand whatever decisions you make inside the Listener code.
Good luck!
 
reply
    Bookmark Topic Watch Topic
  • New Topic