Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2005

Tip: Looking for answers? Try searching our database.

Right-click context menu?

Thread view: 
brightoceanlight@hotmail.com - 12 Mar 2005 16:25 GMT
Is there a way using Java and possibly Swing, to add right-click
context menus to nodes of a tree.  What I am trying to do is add
different right-click context menus when the user selects different
nodes of the tree.  

Thank you,

Gil
John McGrath - 14 Mar 2005 05:02 GMT
> Is there a way using Java and possibly Swing, to add right-click
> context menus to nodes of a tree.  What I am trying to do is add
> different right-click context menus when the user selects different
> nodes of the tree.  

Add a MouseListener to the JTree and on the right-click (actually, you
should use MouseEvent.isPopupTrigger() to recognize this), get the tree
path based on the event coordinates using JTree.getPathForLocation().
Decide what PopupMenu you want to show, and show it.

Signature

Regards,

John McGrath

Fahd Shariff - 14 Mar 2005 17:01 GMT
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... "
John McGrath - 15 Mar 2005 01:29 GMT
> 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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.