Re: Adding popup menu to JMenuItem
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>,
Dan Polansky <dan.polan...@gmail.com> wrote:
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 =
--
Steve W. Jackson
Montgomery, Alabama