JPopupMenu closes when child popup opens

穿精又带淫゛_ 提交于 2019-12-17 14:56:06

问题


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. I've been trying to find a way to override this feature, to no avail.

Does anyone have any suggestions to prevent closing the parent JPopupMenu? Thanks!


回答1:


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



来源:https://stackoverflow.com/questions/8511443/jpopupmenu-closes-when-child-popup-opens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!