JPopupMenu closes when child popup opens

后端 未结 1 504
渐次进展
渐次进展 2020-12-07 02:19

I have a JComboBox (among other components) inside a JPopupMenu. It turns out that whenever I open the combo box\'s popup (to select an item), the parent JPopupMenu closes.

相关标签:
1条回答
  • 2020-12-07 03:02

    that not possible directly, its very hard to override known bug, in other hands Swing doesn't allows two lightwieght popup components in same time

    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());
                }
            });
        }
    }
    

    but workaround is very simple use JWindows or un-decorated JDialog with JComboBox instead of JPopup

    0 讨论(0)
提交回复
热议问题