The name of the menu item in the popup menu depends on the NAME attribute of AbstractAction of that menu item.
When you add some subclass of AbstractAction to the JPopupMenu, you can specify any name to be displayed in the menu by calling super.putValue(NAME, "yourname");
Shown below is an implementation of AbstractAction that does it in the constructor.
public class AddAction extends AbstractAction {
public AddAction(int type)
{
super("Add");
this.type = type;
if(type == 0)
super.putValue(NAME,"Type 0");
else
super.putValue(NAME,"Type 1");
}
}
and add it to the popup menu as follows
JPopupMenu popup = new JPopupMenu();
popup.add(new AddAction(0));
popup.add(new AddAction(1));
here I have added 2 menu items using the same Action but with different names. YOu can ofcourse take different actions based on which menu item is clicked by checking the type in AddAction.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment