我在我的面板上有一个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);
}
}
我不明白为什么showPopup()和setPopupVisible()方法没有按预期运行。