Adding custom elements to a JPopupMenu
-
09-10-2019 - |
Question
The problem can be stated in two different ways:
1. I have a JPopupMenu
where I want to insert a component which is not a JMenuItem
(a custom JTree
)... but I dont know how to do it
or
2. I have a different Popup (JPopupPanel
) and I want to add JMenuItems
below my custom JTree
and that those JMenuItems
work as if they were inserted in a JPopupMenu
: they are highlighted when mouse is over and popup is closed when one of them is selected.
Any hints?
NOTE: in 2. I can add the JMenuItems without problem, show they are shown in the Popup, but they dont act as when they are in a JMenu or JPopupMenu
Solution
For 1. JPopupMenu
supports insertion of any Component
with the method
public void insert(Component component, int index)
For 2. Did you add an ActionListener to the menu?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow