Adding popup menu to JMenuItem

D

Dan Polansky

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
 
S

Steve W. Jackson

Dan Polansky said:
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.
 
D

Dan Polansky

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
 
S

Steve W. Jackson

Dan Polansky said:
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 =
 
D

Dan Polansky

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
 
S

Steve W. Jackson

Dan Polansky said:
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

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 =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top