java 如何在填充 JComboBox 时保持它的弹出菜单打开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2778682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to keep the popup menu of a JComboBox open on populating it?
提问by Stormshadow
I have a JComboBox on my Panel. One of the popup menu items is 'More' and when I click that I fetch more menu items and add them to the existing list. After this, I wish to keep the popup menu open so that the user realizes that more items have been fetched however, the popup closes. The event handler code I am using is as follows
我的面板上有一个 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);
}
}
I do not understand why the showPopup() and setPopupVisible() methods are not functioning as expected.
我不明白为什么 showPopup() 和 setPopupVisible() 方法没有按预期运行。
回答by sreejith
add the following line in the fetchItems method
在 fetchItems 方法中添加以下行
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
box.showPopup();
}
}
}
If u call selectedBox.showPopup(); inside invokelater also it will work.
如果你调用 selectedBox.showPopup(); 在 invokelater 中它也可以工作。
回答by mfidan
overwrite the JCombobox setPopupVisible metod
覆盖 JCombobox setPopupVisible 方法
public void setPopupVisible(boolean v) {
if(v)
super.setPopupVisible(v);
}
回答by Rachid Chalouli
jComboBox1 = new javax.swing.JComboBox(){
@Override
public void setPopupVisible(boolean v) {
super.setPopupVisible(true); //To change body of generated methods, choose Tools | Templates.
}
};
};
回答by mad_lobster
I found some simple solution to always keep popup open. It may be useful with some custom JComboBox'es, like the one I have in my project, but is a little hacky.
我找到了一些简单的解决方案来始终保持弹出窗口打开。它可能对一些自定义 JComboBox 很有用,就像我在我的项目中所拥有的那样,但有点麻烦。
public class MyComboBox extends JComboBox
{
boolean keep_open_flag = false; //when that flag ==true, popup will stay open
public MyComboBox(){
keep_open_flag = true; //set that flag where you need
setRenderer(new MyComboBoxRenderer()); //our spesial render
}
class MyComboBoxRenderer extends BasicComboBoxRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (index == -1){ //if popup hidden
if (keep_open_flag) showPopup(); //show it again
}
}
}
}

