简体   繁体   English

JPopupMenu 上的 JComboBox

[英]JComboBox on a JPopupMenu

I'm trying to use a compound Swing component as part of a Menu.我正在尝试使用复合 Swing 组件作为菜单的一部分。

Everything works just fine, apart from one detail: The component contains JComboBox es and whenever the user clicks on one of them to open its dropdown, the dropdown opens but the menu disappears.一切正常,除了一个细节:该组件包含JComboBox es,每当用户单击其中一个以打开其下拉菜单时,下拉菜单就会打开,但菜单会消失。 Is it possible to make the menu stay open when a JComboBox is clicked?单击JComboBox时是否可以使菜单保持打开状态?

I sub-classed JMenu .我对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 the custom compound component. FilterPanel是自定义的复合组件。 The pm.pack() is called to adapt the size of the JPopupMenu when the filterPanel changes in size.filterPanel的大小发生变化时,调用pm.pack()来调整JPopupMenu的大小。

Thanks for your help谢谢你的帮助

are you meaning this bug你是说这个bug

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

Look at Jide OSS' PopupWindow .看看 Jide OSS 的PopupWindow This provides an easy-to-use solution for this problem.这为这个问题提供了一个易于使用的解决方案。 Works fine for me.对我来说很好。

Javadoc is here . Javadoc 在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM