PopupMenu in JMenuItem
Hi. I have a JMenu which has several JMenuItems added to it. When an item is right-clicked, I would like a JPopupMenu to be displayed. This can be done by creating a custom MenuUI and only firiing actions on left clicks and showing the popup menu on right-click.
The problem is that when the popup menu appears the underlying menu disappears, which I do not want to happen (I'm trying to achieve the effect of a favorites menu in a web browser where you can right click a favorite item and edit, delete, etc).
Does anyone have a good way to do this?
Thanks,
Jeff
[594 byte] By [
jastoreya] at [2007-11-26 16:58:46]

# 2
This is the only way I can think of, but it's a nasty hack.
You can add your popup code in the else of the if in mousePressed.
setUI(new BasicMenuUI() {
MouseInputListener listener;
protected MouseInputListener createMouseInputListener(JComponent c) {
listener = super.createMouseInputListener(c);
return new MouseInputListener() {
public void mouseClicked(MouseEvent e) {
listener.mouseClicked(e);
}
public void mousePressed(MouseEvent e) {
if (!(e.getButton() == MouseEvent.BUTTON3))
listener.mousePressed(e);
}
public void mouseReleased(MouseEvent e) {
listener.mouseReleased(e);
}
public void mouseEntered(MouseEvent e) {
listener.mouseEntered(e);
}
public void mouseExited(MouseEvent e) {
listener.mouseExited(e);
}
public void mouseDragged(MouseEvent e) {
listener.mouseDragged(e);
}
public void mouseMoved(MouseEvent e) {
listener.mouseMoved(e);
}
};
}
});
# 3
>
> But a list of favorites is normally not editable in
> the actual menu. You are looking a list or a tree
> normally, and that is where the popup appears on a
> right click.
The Bookmarks menu in Firefox is certainly editable in the way the OP describes, as is the Favorites menu in IE7.