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

 
Doug Gschwind
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic