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 / General / May 2007

Tip: Looking for answers? Try searching our database.

Adding popup menu to JMenuItem

Thread view: 
Dan Polansky - 16 May 2007 17:01 GMT
How do I add a popup menu to JMenuItem? Has anyone a link or
experience with something of the sort? I tried adding a mouse listener
to the JMenuItem, but the JMenuItem kept performing its action even
when I have reacted to the right click and consumed the mouse event.

Thank you, Dan
Steve W. Jackson - 16 May 2007 18:36 GMT
> How do I add a popup menu to JMenuItem? Has anyone a link or
> experience with something of the sort? I tried adding a mouse listener
> to the JMenuItem, but the JMenuItem kept performing its action even
> when I have reacted to the right click and consumed the mouse event.
>
> Thank you, Dan

Perhaps explain more about what you're actually trying to accomplish.

A menu item is designed to be selected and result in some action.  If
you're looking to have a "submenu" then you shouldn't be using JMenuItem
but JMenu instead.
Signature

Steve W. Jackson
Montgomery, Alabama

Dan Polansky - 16 May 2007 22:22 GMT
Hello Steve, I am trying to offer a popup menu that will offer the
option to delete
the JMenuItem. That is why a submenu does not do. A popup menu like
that
can be seen for instance for the items in the bookmarks menu of
FireFox.
When you right-click an item in the bookmarks menu, a popup lets you
open the bookmark, view its properties, and more. --Dan

On May 16, 7:36 pm, "Steve W. Jackson" <stevewjack...@knology.net>
wrote:
> In article <1179331306.964489.250...@n59g2000hsh.googlegroups.com>,
>
[quoted text clipped - 13 lines]
> Steve W. Jackson
> Montgomery, Alabama
Steve W. Jackson - 17 May 2007 16:09 GMT
> Hello Steve, I am trying to offer a popup menu that will offer the
> option to delete the JMenuItem. That is why a submenu does not do. A
> popup menu like that can be seen for instance for the items in the
> bookmarks menu of FireFox. When you right-click an item in the
> bookmarks menu, a popup lets you open the bookmark, view its
> properties, and more. --Dan

FYI, that Firefox behavior is platform-specific.  It does not work on my
Mac.

You might try a MouseListener on that JMenuItem instead of an Action or
the traditional ActionListener.  In the mouseClicked method you'd be
able to determine the modifiers on the click that activated it.  If it's
a regular click (typically left-button), simply invoke the response that
the menu item would on its own.  Otherwise, create your contextual menu.  
Of course, that might not be such a good idea if you've got
Windows-style mnemonics and keyboard navigation/selection of menu items.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Dan Polansky - 18 May 2007 15:21 GMT
Hello Steve, I did not realize the behavior is platform specific.

Anyway, instead of relying on right mouse-click, I have implemented a
special behavior of a JMenuItem on holding the "Control" key, which
looks as follows. I was tired of searching for a solution using the
right click. (Just for the case anyone is interested.)

  AbstractAction action = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
           if ((e.getModifiers() & ActionEvent.CTRL_MASK) == 0) {
              // [ Key "Control" not held ]
              // The normal course of action of the JMenuItem.
              ...
              return; }
           // [ Key "Control" held ]
           // The special course of action, poping up windows, and
the like.
           ...
  JMenuItem item = new JMenuItem(action);
  ...

--Dan

On May 17, 5:09 pm, "Steve W. Jackson" <stevewjack...@knology.net>
wrote:
> In article <1179350558.376300.70...@u30g2000hsc.googlegroups.com>,
>
[quoted text clipped - 20 lines]
> Steve W. Jackson
> Montgomery, Alabama
Steve W. Jackson - 18 May 2007 17:39 GMT
> Hello Steve, I did not realize the behavior is platform specific.
>
[quoted text clipped - 18 lines]
>
> --Dan

Interesting solution.  But I'll point out in the interest of
cross-platform considerations that the Control key isn't necessarily the
right one on a Mac.  :-)

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama



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.