JComboBox 弹出菜单不出现

发布于 2024-10-15 22:00:04 字数 953 浏览 3 评论 0原文

我在 JPanel 中有一个 JComboBox(它本身嵌套在其他几个 JPanel 中)。它填充了枚举的成员。我遇到了一个问题,当我单击展开按钮时,弹出菜单不会出现。

以下是我迄今为止收集到的信息:

1) 第一次单击展开按钮不会执行任何操作。第二次单击突出显示了该框的内容,但弹出窗口仍然没有出现。

2)一旦我单击按钮并赋予其焦点,向上/向下击键就会正确地循环浏览条目。

3)我尝试添加 PopupMenuListener 来打印事件,并且事件以无法解释的方式触发:

mouse down: popupMenuWillBecomeVisible fires
mouse up: nothing
mouse down: popupMenuWillBecomeInvisible fires
mouse up: nothing

这是我的 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);

有谁知道为什么弹出菜单可能不会出现?

感谢您的帮助!

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) 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) Once I've clicked the button and given it focus, up/down keystrokes cycle through the entries correctly.

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

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?

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫烠⑼条掵仅有一顆心 2024-10-22 22:00:04

我在 mac (10.5+) 上遇到了 Java (1.6) 的类似问题
这是因为 jCombobox 出现的对话框是
模态。

就我而言,下拉列表实际上确实出现了,但因为它没有那么大
它是在对话框后面绘制的并不明显:)所以如果你
怀疑是这种情况,通过使下拉菜单变长来验证它
足以出现在隐藏它的组件下方。

  • 用许多填充下拉列表
    项目。
  • 设置其规范以返回
    足够大的数量
    “获取最大行计数”。

希望有帮助。

I've had the similair problem with Java (1.6) on mac (10.5+)
and it was due to the dialog the jCombobox appeared in was
modal.

In my case the dropdown actually DID appear but as it was not that large
it was not obvious that it was drawn behind the dialog :) so if you
suspect this to be the case verify it by making the dropdown be long
enough to appear below the component hiding it.

  • populate the dropdown with many
    items.
  • set its Specification to return a
    large enough number on
    "getMaximumRowCount".

Hope it helps.

网白 2024-10-22 22:00:04

我遇到了同样的问题,想知道出了什么问题,因为通常它是有效的,而且这种错误会非常非常严重。

因此,我删除了所有带有 JDialog 的主方法(其中仅包含 JComboBox)的内容。

当我删除所有内容时,我意识到自己做错了什么:我使用了 getRootPane() 而不是 getContentPane()

我通常使用 ESC 关闭对话框并将 KeyStroke 添加到 rootPane。这就是我粗心犯下错误的原因。

我希望这能帮助下一个犯同样错误的人:)

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.

So I deleted everything that I just had a main-method with an JDialog with just an JComboBox within it.

The moment I had deleted everything I realized what I've done wrong: I used getRootPane() instead of getContentPane().

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 : )

口干舌燥 2024-10-22 22:00:04

我不确定您的情况是否会发生这种情况,但很大一部分与 UI 相关的问题是由于 UI 代码未在事件调度线程中运行造成的。 Swing 要求所有代码都应在此线程中运行,否则您可能会遇到 GUI 看起来不正确或事件未正确触发等问题。如果您在事件调度线程中运行所有这些代码,那么您可以忽略这一点,否则请查看此链接:

http://download.oracle.com/javase /tutorial/uiswing/concurrency/dispatch.html

I'm not sure if this is happening in your case, but a good portion of UI-related issues are due to the UI code not being run in the Event Dispatching Thread. Swing mandates that all code should be run in this thread or else you might suffer issues with GUIs not looking right or events not firing off properly, etc. If you are running all of this code in the Event Dispatching Thread then you can disregard this, otherwise check out this link:

http://download.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文