Java JPanel 重绘问题
我有一个 Java swing 应用程序,其面板包含三个无法正确绘制的 JComboBoxe
。
组合框仅显示为右侧的向下箭头,但没有当前所选值的标签。
如果窗口的大小调整得更大或更小,即使是一个像素,这些框也会正确地重新绘制。
我所有的谷歌搜索都指向在 JPanel
上调用 revalidate()
来解决这个问题,但这对我来说不起作用。
在 JPanel
上调用 updateUI()
已将其从始终显示不正确更改为一半时间显示不正确。
有没有其他人看到过这一点并找到了强制重绘组合框的不同方法?
I have a Java swing application with a panel that contains three JComboBoxe
s that do not draw properly.
The combox boxes just show up as the down arrow on the right side, but without the label of the currently selected value.
The boxes will redraw correctly if the window is resized either bigger or smaller by even one pixel.
All of my googling has pointed to calling revalidate()
on the JPanel
to fix this, but that hasn't worked for me.
Calling updateUI()
on the JPanel
has changed it from always displaying incorrectly to displaying incorrectly half of the time.
Has anyone else seen this and found a different way to force a redraw of the combo boxes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否向我们提供有关如何将组合框添加到 JPanel 的更多信息? 这是 Swing 中很常见的事情,所以我怀疑这是 JVM 问题,但我想一切皆有可能。
具体来说,我会仔细检查以确保您没有从任何后台线程访问 GUI。 在这种情况下,也许您正在从数据库或其他东西读取选择,并从后台线程更新 JComboBox,这在 Swing 中是一个很大的禁忌。 请参阅 SwingUtils.invokeLater()。
Can you give us some more information on how you add the combo boxes to the JPanel? This is a pretty common thing to do in Swing so I doubt that it's a JVM issue but I guess anything is possible.
Specifically, I would double check to make sure you're not accessing the GUI from any background threads. In this case, maybe you're reading the choices from a DB or something and updating the JComboBox from a background thread, which is a big no-no in Swing. See SwingUtils.invokeLater().