File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTree expansion/collapse callback Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree expansion/collapse callback" Watch "JTree expansion/collapse callback" New topic

JTree expansion/collapse callback

Doug Gschwind
Ranch Hand

Joined: Dec 17, 1998
Posts: 44
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

Joined: Jan 30, 2000
Posts: 18671
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!

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: JTree expansion/collapse callback
jQuery in Action, 3rd edition