[Solved] Add a menu item within a menu within a menu item in JAVA


You can nest JMenu within other instances of JMenu, and you can add a JMenuItem to a JMenu, but you can’t add a JMenuItem to another JMenuItem. Think about that… what did you expect that to do? A leaf, for example, grows from a branch, not from another leaf.

JMenu menu = new JMenu("Main Menu");

JMenu submenu = new JMenu("A menu within a menu");

JMenu subsubmenu = new JMenu("A menu within a menu within a menu");

JMenuItem itemFoo = new JMenuItem("Foo");
JMenuItem itemBar = new JMenuItem("Bar");
JMenuItem itemBaz = new JMenuItem("Baz");
JMenuItem itemQuz = new JMenuItem("Quz");

// These are OK

menu.add(subMenu);
subMenu.add(subsubmenu);
subMenu.add(itemFoo);
subMenu.add(itemBar);
subMenu.add(itemBaz);

// NO!

itemBaz.add(itemQuz);

Review the tutorial.

4

solved Add a menu item within a menu within a menu item in JAVA