Build a JPopupMenu and add a MouseListener to the tree.
tree.addMouseListener (new MouseAdapter() {
public void mousePressed (MouseEvent e) {
if (e.isPopupTrigger()) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
//based on the node, change the popup and
display
popup.show (e.getComponent(),
e.getX(), e.getY());
}
}
}) ;
I hope you get the idea.
--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking... "
> Build a JPopupMenu and add a MouseListener to the tree.
>
[quoted text clipped - 10 lines]
> }
> }) ;
There are a few problems with this:
1) This checks isPopupTrigger() only on mousePressed(). The point of
using isPopupTrigger() is that the event that should trigger a popup is
different on different platforms. For example, on Windows, popups are
triggered on the mouseReleased event. As a result, the above code would
not do anything on Windows.
I usually write popup recognizers like this:
private MouseListener mouseListener = new MouseAdapter() {
public void mousePressed( MouseEvent event ) {
handlePopup( event );
}
public void mouseReleased( MouseEvent event ) {
handlePopup( event );
}
private void handlePopup( MouseEvent event ) {
if ( event.isPopupTrigger() ) {
// Show the popup
}
}
}
2) The code uses tree.getLastSelectedPathComponent() to determine which
node should be used to select the popup menu. This assumes that the node
that was clicked on will be last selected node. Even if you knew that
would be the case, I would argue that it would be better to use the event
coordinates to determine the node, since that would be depending on
external knowledge.
However, in this case, it is not true that the node will be the last
selected node. A right-click on a node does *not* select it, at least on
Windows using the default-look-and-feel. So you should definitely use the
event coordinates.
if ( e.isPopupTrigger() ) {
TreePath path = tree.getPathForLocation( e.getX(), e.getY() );
if ( path != null ) {
TreeNode node = (TreeNode) path.getLastPathComponent();
JPopupMenu popup = createPopup( node );
popup.show( e.getComponent(), e.getX(), e.getY() );
}
}
That said, I think it is a good idea to select the node that is clicked
on. The user will find it confusing if one node is selected, but the
popup for a different node is shown. So you might want to add something
like this:
if ( e.isPopupTrigger() ) {
TreePath path = tree.getPathForLocation( e.getX(), e.getY() );
if ( path != null ) {
tree.getSelectionModel().setSelectionPath( path );
TreeNode node = (TreeNode) path.getLastPathComponent();
JPopupMenu popup = createPopup( node );
popup.show( e.getComponent(), e.getX(), e.getY() );
}
}
Of course, now that you have guaranteed that the node you want is
selected, you could use getLastSelectedPathComponent() to get the node.
However, you already have the path for that node, and
getLastSelectedPathComponent() gets that path from the selection model and
calls getLastPathComponent() on it, so there is no reason not to just get
the node from the path that you already have.

Signature
Regards,
John McGrath
Denis Krukovsky - 15 Mar 2005 21:25 GMT
>>Build a JPopupMenu and add a MouseListener to the tree.
>>
[quoted text clipped - 79 lines]
> calls getLastPathComponent() on it, so there is no reason not to just get
> the node from the path that you already have.
Is it OK to provide web links in news? Take a look here
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=538065
http://dotuseful.sourceforge.net/doc/javadoc/org/dotuseful/ui/tree/MouseAdaptedT
ree.html

Signature
Denis Krukovsky
http://dotuseful.sourceforge.net/