Dariush Jafari
Dariush Jafari

Reputation: 5443

java multi level JPopupMenu

I've created a JPopupMenu with two items (add,remove). I want "addItem" to have a sub popup menu. The hierarchy is like this:

add
  pizza
  cake
  ...
remove

my code:

JPopupMenu menu = new JPopupMenu();
menu.add(new JMenuItem("remove"));
JMenuItem addItem = new JMenuItem("add");
menu.add(addItem);

addItem.add(new JPopupMenu()); // it is not working for me

Once I move the mouse close to "add item", the menu disappears.
please help me to build this popup menu.

Upvotes: 1

Views: 964

Answers (1)

Dariush Jafari
Dariush Jafari

Reputation: 5443

Use a JMenu (sub class of JMenuItem).

JPopupMenu menu = new JPopupMenu();
menu.add(new JMenuItem("remove"));
JMenuItem addItem = new JMenu("add");
menu.add(addItem);

addItem.add(new JMenuItem("pizza"));
addItem.add(new JMenuItem("cake"));

enter image description here

Upvotes: 1

Related Questions