How can I give my JMenuItem
s names that the ActionListener
attached to them will look at?
I've got a menu system that's handled by a single ActionListener
, and some items in those menus duplicate names. This isn't a problem on the user end, because it's obvious what does what; in fact, it would be more confusing if they had different names. However, at my end, I want to label each item uniquely.
The section that creates my items looks like this:
String label = getLabel(forThisItem);
JMenuItem item = new JMenuItem(label);
item.setName(parentMenu.getName() + "_" + label);
item.addActionListener(actionListener);
parentmenu.add(item);
Interrogating the item afterwards (and outside the scope of this method) with getName() gives the name I gave it, as it should, but the output of
public void actionPerformed(ActionEvent ae) {
String actionPerformed = ae.getActionCommand();
System.out.println("actionPerformed: " + actionPerformed);
}
is the the, possibly duplicated, name that the user sees, specified by label
, not the unique name that I gave it.
How can I give the right information to the ActionListener?