JComboBox on JPopupMenu

I am trying to use the Swing component as part of a menu.

Everything works fine except for one detail: The component contains JComboBox es, and whenever a user clicks on one of them to open a drop-down list, a drop-down menu opens, but the menu disappears. Can I open the menu when I click JComboBox ?

I am subclassed by JMenu . This is the corresponding code:

 public class FilterMenu extends JMenu { public FilterMenu(String name) { super(name); final JPopupMenu pm = this.getPopupMenu(); final FilterPanel filterPanel = new FilterPanel(pm) { @Override public void updateTree() { super.updateTree(); pm.pack(); } }; pm.add(filterPanel); } } 

FilterPanel is a custom composite component. pm.pack() is called to adapt the size of JPopupMenu when the FilterPanel changes in size.

thanks for the help

+6
source share
2 answers

Do you mean this error

 import javax.swing.*; import java.awt.event.*; public class Test { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(400, 400); frame.setVisible(true); String[] list = {"1", "2", "3", "4",}; JComboBox comb = new JComboBox(list); final JPopupMenu pop = new JPopupMenu(); pop.add(comb); frame.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { System.out.println("mousePressed"); pop.show(e.getComponent(), e.getX(), e.getY()); } }); } } 
+2
source

Take a look at Jide OSS ' PopupWindow . This provides an easy-to-use solution to this problem. Works great for me.

Javadoc is here .

+1
source

Source: https://habr.com/ru/post/891885/


All Articles