Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update JMenu display names

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.

like image 543
John Powers Avatar asked Nov 15 '11 21:11

John Powers


People also ask

What is a JMenuBar?

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).

Which method is used to change the name of a menu item in Java?

Use MenuItem. setTitle(). If this isn't what you needed, you have to be more specific.

What is the use of JMenuItem in Java?

Creates a menu item whose properties are taken from the specified Action.

What is menu and menu item in Java?

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.


1 Answers

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.

like image 161
yair Avatar answered Oct 12 '22 21:10

yair