如何在填充 JComboBox 时保持它的弹出菜单打开?

Sto*_*dow 4 java applet swing combobox jcombobox

我的面板上有一个 JComboBox。弹出菜单项之一是“更多”,当我单击它时,我会获取更多菜单项并将它们添加到现有列表中。在此之后,我希望保持弹出菜单打开,以便用户意识到已获取更多项目,但是弹出菜单关闭。我使用的事件处理程序代码如下

public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == myCombo) {
            JComboBox selectedBox = (JComboBox) e.getSource();
            String item = (String) selectedBox.getSelectedItem();
            if (item.toLowerCase().equals("more")) {
                fetchItems(selectedBox);
            }
            selectedBox.showPopup();
            selectedBox.setPopupVisible(true);
        }
    }



private void fetchItems(JComboBox box)
    {
        box.removeAllItems();
        /* code to fetch items and store them in the Set<String> items */
        for (String s : items) {
            box.addItem(s);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我不明白为什么 showPopup() 和 setPopupVisible() 方法没有按预期运行。

sre*_*ith 7

在 fetchItems 方法中添加以下行

SwingUtilities.invokeLater(new Runnable(){

    public void run()
    {

       box.showPopup();
    }
Run Code Online (Sandbox Code Playgroud)

}

如果你调用 selectedBox.showPopup(); 在 invokelater 中它也可以工作。