Update JMenu Display Names

So, I have JMenu with several submenus inside. The names of these menus are set by obtaining the name of 1 out of 4 players. I added MenuListener to JMenu to update these names using

menu.setName(player.getName()); 

However, the name changes, but the update does not appear on the menu. How do I display a menu for updating?

 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 indicates that the name has changed.

+6
source share
1 answer

If I understand exactly what you want (a snapshot might be useful), then you should use menu.setText("player1")

setName(string) not displayed, see here .

+5
source

Source: https://habr.com/ru/post/901569/


All Articles