Question

I'm doing an assignment regarding GUI's in java, and I have an issue.

I'm supposed to create a nested menu. That's fine and I can do that, but I've come across an issue.

If I add the same JMenuItem to two JMenu objects, it only shows up on one of the menus when I run the program.

EG: object1 and object2 are JMenu objects. color1 is a JMenuItem object.

object1.add(color1);
object2.add(color1);

When this happens and I compile the program to check out the GUI, only object2 has color1 listed.

How can I make it so that both object1 and object2 menus display color1 on the GUI? Is there a way to do that without having to create a large amount of JMenu objects?

Was it helpful?

Solution

An instance of a Component (or in your case JMenuItem) can only belong to a single parent (or in your case JMenu).

You will need to create new instances of the JMenuItem for each menu you want to add them to...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top