JComboBox弹出菜单没有出现 [英] JComboBox popup menu not appearing
问题描述
我在JPanel中有一个JComboBox(它本身嵌套在其他一些JPanel中)。它包含了一个枚举成员。我遇到了一个问题,当我点击展开按钮时没有出现弹出菜单。
I have a JComboBox inside a JPanel (which itself is nested within a few other JPanels). It's populated with members of an enum. I'm running into a problem where the popup menu doesn't appear when I click the expand button.
这是我到目前为止收集的信息:
Here's the information I've gathered so far:
1)首次点击展开按钮不会做任何事情。第二次点击会突出显示该框的内容,但弹出窗口仍然没有显示。
1) The first click on the expand button does nothing. The second click highlights the contents of the box, but the popup still doesn't appear.
2)一旦我点击了按钮并给予它焦点,就/向下击键可以正确地循环输入条目。
2) Once I've clicked the button and given it focus, up/down keystrokes cycle through the entries correctly.
3)我尝试添加PopupMenuListener来打印事件,事件以无法解释的方式触发:
3) I've tried adding a PopupMenuListener to print out events, and the events fire in unexplained ways:
mouse down: popupMenuWillBecomeVisible fires
mouse up: nothing
mouse down: popupMenuWillBecomeInvisible fires
mouse up: nothing
这是我对JComboBox的初始化代码:
Here's my initialization code for the JComboBox:
comboBox = new JComboBox();
comboBox.setPreferredSize(new Dimension(175, 30));
comboBox.setMaximumSize(new Dimension(175, 30));
comboBox.setAlignmentX(0.5f);
comboBox.addItem(Enum.Value1);
...
parentPanel = new JPanel();
parentPanel.setLayout(new BoxLayout(parentPanel, BoxLayout.X_AXIS));
parentPanel.setMaximumSize(new Dimension(37267, 50));
... add some other stuff to parentPanel ...
parentPanel.add(comboBox);
有没有人知道为什么弹出菜单可能不会出现?
Does anyone have any idea why the popup menu might not be appearing?
感谢您的帮助!
推荐答案
我遇到了同样的问题,并想知道出了什么问题,因为通常它是有效的,而且那种bug非常非常关键。
I had the same issue and was wondering what was wrong, because usually it works and that kind of bug would be very very critical.
所以我删除了所有我只有一个带有JComboBox的JDialog的main方法的内容它。
So I deleted everything that I just had a main-method with an JDialog with just an JComboBox within it.
当我删除所有内容时,我意识到我做错了什么:我使用了 getRootPane()
而不是 getContentPane()
。
The moment I had deleted everything I realized what I've done wrong: I used getRootPane()
instead of getContentPane()
.
我通常使用ESC关闭对话框并将KeyStroke添加到rootPane。这就是我不小心弄错的原因。
I usually use ESC for closing the dialog and add the KeyStroke to the rootPane. That was the reason for my careless mistake.
我希望这会帮助下一个犯错的人:)
I hope that will help the next ones doing that mistake : )
这篇关于JComboBox弹出菜单没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!