Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JPopupMenu JMenuItem's are not firing their actions

1 view
Skip to first unread message

onk...@gmail.com

unread,
May 20, 2006, 10:08:47 PM5/20/06
to
I have a JPopupMenu that is not part of a JMenuBar, it is brought up
next to a button in the JFrame when pushed. Using a MouseListener, the
button show()s the JPopupMenu when the button is pressed, and the
JPopupMenu disappears when the button is released. The mouse can be
dragged to the menu items and they do highlight correctly. However,
when the mouse is released on the items, they do not fire their
commands.

An alternative method of making the menu stay open when clicked so that
the use can completely press and release on one of the JMenuItems also
did not work.

in pieces,

this is the creation code of the button that shows the popup menu.
headerPanel[i].add(new ResultEditMenu(i,this));

where ResultEditMenu extends JButton and implements MouseListener

//here is my constructor:
public ResultEditMenu(int i,ActionListener al) {
super(">"); // the button text
this.setPreferredSize(new Dimension(17,17));
this.setMargin(new Insets(0,0,0,0));
this.addMouseListener(this); // the button responds to pressing and
releasing by showing the popup menu
index = i;
externalListener = al; // this is the actionListener that should
respond to the menu items
menu = new JPopupMenu();
menu.add(Win.makeMenuItem("Transfer",externalListener,"RESULTWND_TRANSFER_"
+ index));
menu.add(Win.makeMenuItem("Edit",externalListener,"RESULTWND_EDIT_" +
index));
menu.add(Win.makeMenuItem("Delete",externalListener,"RESULTWND_DELETE_"
+ index));
}

//Win.makeMenuItem is a (static) shortcut method I wrote that sets the
name, the actionlistener, and the actioncommand of the menu item
respectively then returns it for adding to the menu

//here are two the methods for ResultEditMenu to implement
MouseListener
public void mousePressed(MouseEvent e) {
menu.show(this,this.getWidth(),-2); // the popup menu is shown right
next to this button
this.setText(" "); // makes it more obvious which button was pressed
}
public void mouseReleased(MouseEvent e) {
menu.setVisible(false);
this.setText(">"); //restore original label
}

//this is the externalListener actionPerformed() method that should
respond to the menu items
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand()); // does not print anything
when the menu items are clicked
}

I hope I've provided enough information. Thank you for reading.
~Jeff

pit.g...@gmx.de

unread,
May 21, 2006, 10:58:28 AM5/21/06
to
onk...@gmail.com schrieb:

> I have a JPopupMenu that is not part of a JMenuBar, it is brought up
> next to a button in the JFrame when pushed. Using a MouseListener, the
> button show()s the JPopupMenu when the button is pressed, and the

> JPopupMenu disappears when the button is released. The mouse can be
> dragged to the menu items and they do highlight correctly. However,
> when the mouse is released on the items, they do not fire their
> commands.
>
> An alternative method of making the menu stay open when clicked so that
> the use can completely press and release on one of the JMenuItems also
> did not work.

So we have two snippets that will show the popupmenu, but none of them
will make the menu items fire their events?
>
> in pieces,
<text snipped>
>

> I hope I've provided enough information.

Well, I haven´t overlooked something, then you seem to have provided
the code for the stuff that is working (show the button upon
mouseclick, show the menu when the buton is pressed asf), but you
haven´t provided the code for the part that is not working:
registering the action listener with the menu items. Please provide a
short, compilable example or at least the source for Win.makeMenuItem.
Regards, Piet

Jeff

unread,
May 21, 2006, 4:07:24 PM5/21/06
to
You're right, that wasn't included. However, I've used the same code
in other places in the program and it works there, and I just verified
this.

public static JMenuItem makeMenuItem(String str,ActionListener
al,String com) {
JMenuItem menuItem = new JMenuItem(str);
menuItem.setFont(desiredFont);
menuItem.addActionListener(al);
menuItem.setActionCommand(com);
return menuItem;
}

Sorry for leaving this out before. Thanks for reading.

Jeff

Jeff

unread,
May 22, 2006, 6:12:35 PM5/22/06
to
Well, I'm gonna do it a different way now that I've had some time to
brainstorm and thought of a better interface implementation. I'm gonna
make the popup be a separate window with the "Transfer" "Edit" and
"Delete" options operating essentially like a tabbed pane. I think
this will be more fluid.

Thanks for reading.
~Jeff

0 new messages