So I have a JMenu with a few submenus inside. The names of those menus are set by getting the name of a 1 of 4 players. I added a MenuListener to the JMenu to update those names using
menu.setName(player.getName());
However, the name is changing but the update is not displaying in the menu. How do I get the menu to update it's display?
editMenu.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent e) {
updateMenu();
}
public void menuDeselected(MenuEvent e) {
}
public void menuCanceled(MenuEvent e) {
}
});
and updateMenu method:
public void updateMenu()
{
partOneMenu.setName(Participant1.getName());
partTwoMenu.setName(Participant2.getName());
partThreeMenu.setName(Participant3.getName());
partFourMenu.setName(Participant4.getName());
partOneMenu.revalidate();
partTwoMenu.revalidate();
partThreeMenu.revalidate();
partFourMenu.revalidate();
System.out.println(partOneMenu.getName());
}
The print statement is showing that the name has changed.
JMenuBar is an implementation of menu bar . the JMenuBar contains one or more JMenu objects, when the JMenu objects are selected they display a popup showing one or more JMenuItems . JMenu basically represents a menu . It contains several JMenuItem Object . It may also contain JMenu Objects (or submenu).
Use MenuItem. setTitle(). If this isn't what you needed, you have to be more specific.
Creates a menu item whose properties are taken from the specified Action.
The object of MenuItem class adds a simple labeled menu item on menu. The items used in a menu must belong to the MenuItem or any of its subclass. The object of Menu class is a pull down menu component which is displayed on the menu bar. It inherits the MenuItem class.
If I understand what exactly you want (a snapshot could have been useful), then you should use menu.setText("player1")
setName(string)
is not for display, see here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With