javaswingjmenuitemjpopupmenu

Can you change the order of JMenuItems?


This a really straightforward question. Can I change the order of JMenuItems in a JPopupMenu? Or is there a way to specify something like an index when adding a menu item to a popup menu like you can with a list (e.i. List.add(int, Object))?

Context: I have an abstract class which is adding a menu item to a popup menu in the super constructor. I want to insert menu items before that item in the child class' constructor.

Any easy way to do this?


Solution

  • Have you tried the following method of JPopupMenu?

    void insert(Component component, int index)