You need to add the MenuListener to the menu item that you want to be dynamic. In the void menuSelected (MenuEvent e) method, implement submenu building. In the first implementation, you can first reset the contents of your menu, and then rebuild it from scratch, rather than updating it:
JMenu menu = new JMenu("Bookmarks");
menu.addMenuListener(new MyMenuListener());
private class MyMenuListener implements MenuListener {
public void menuCanceled(MenuEvent e) { }
public void menuDeselected(MenuEvent e) { }
public void menuSelected(MenuEvent e) {
JMenu menu = (JMenu) e.getSource();
populateWindowMenu(menu);
}
}
void populateWindowMenu(JMenu windowMenu) {
windowMenu.removeAll();
}
source
share