Swing 应用程序和模态对话框的渲染问题
我有一个 Java/Swing 桌面应用程序(Windows XP 上的 Java 6u16),有时用户会发现它挂起。我说似乎是因为实际上发生的情况是应用程序正在显示模式对话框,但该对话框并未被渲染。如果用户使用 Alt-Tab 离开应用程序然后返回到应用程序,则对话框将正确呈现。此外,如果远程用户通过 NetOp(类似于 VNC/远程桌面)连接到会话,这也会导致 GUI 被正确重绘。
该应用程序通过 JavaWebstart 运行。由于我听说 DirectDraw 引起渲染问题,因此我在 JNLP 中添加了以下内容,
<property name="sun.java2d.noddraw" value="true"/>
但问题仍然出现(如果我理解正确,这将完全关闭 DirectDraw 和 Direct3d:请参阅 http://download.oracle.com/javase/1.5.0/docs/ guide/2d/flags.html#noddraw)
我对此没有想法,任何建议将不胜感激。
谢谢,
Phil
编辑...
我有一个抽象对话框类,它扩展了 JDialog 并扩展了所有其他对话框。它包含以下方法:
public void showDialog() {
initKeyBindings();
Application.getApplication().deactivateScannerListener();
setVisible(true);
}
每当我想要显示对话框时,我都会调用 showDialog()。 initKeyBindings 方法设置一个 ActionMap,而第二行是特定于应用程序的(应用程序是单例,我在显示对话框时禁用 JPOS 扫描器侦听器)。
有一个相应的 hideDialog() 方法如下:
public void hideDialog() {
setVisible(false);
Application.getApplication().activateScannerListener();
dispose();
}
谢谢, 菲尔
编辑... 对此感到抱歉,还有一项编辑:所有对话框都有一个父级。如果没有指定其他父级,AbstractDialog 类将默认为主应用程序框架。
供参考 对于关注此内容的任何人,我已将以下内容添加到我的代码中:
if (SwingUtilities.isEventDispatchThread()) {
initialiseAndShowDialog();
} else {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
initialiseAndShowDialog();
}
});
}
这可确保仅从 EDT 打开该对话框。
I have a Java/Swing desktop application (Java 6u16 on Windows XP) which occasionally appears to the users to hang. I say appears to because in reality what is happening is that the application is showing a modal dialog but this dialog is not being rendered. If the user uses Alt-Tab to switch away from the application and then subsequently returns to it, the dialog gets rendered correctly. Additionally, if a remote user connects to the session via NetOp (a VNC/Remote Desktop workalike) this also causes the GUI to be redrawn correctly.
The app runs via JavaWebstart. Since I've heard of rendering issues being caused by DirectDraw, I added the following to the JNLP
<property name="sun.java2d.noddraw" value="true"/>
but the problem still occurs (If I have understood correctly, this will switch off DirectDraw and Direct3d completely: see http://download.oracle.com/javase/1.5.0/docs/guide/2d/flags.html#noddraw)
I'm out of ideas on this one, any suggestions would be greatly appreciated.
Thanks,
Phil
Edit...
I have an abstract dialog class which extends JDialog and which all other dialogs extend. It contains the following method:
public void showDialog() {
initKeyBindings();
Application.getApplication().deactivateScannerListener();
setVisible(true);
}
Whenever I want to display a dialog, I call showDialog(). The initKeyBindings method sets up an ActionMap while the second line is application specific (Application is a singleton, I'm disabling the JPOS scanner listener while the dialog is displaying).
There is a corresponding hideDialog() method as follows:
public void hideDialog() {
setVisible(false);
Application.getApplication().activateScannerListener();
dispose();
}
Thanks,
Phil
Edit...
Sorry about this, one more edit: all of the dialogs have a parent. The AbstractDialog class will default to the main application frame if no other parent is specified.
FYI
For anyone following this, I've added the following to my code:
if (SwingUtilities.isEventDispatchThread()) {
initialiseAndShowDialog();
} else {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
initialiseAndShowDialog();
}
});
}
This ensures that the dialog is only opened from the EDT.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您从哪个线程调用 showDialog()? Swing 组件只能在事件调度线程上访问。
您可以尝试 SwingUtilities.invokeAndWait()
并且传递给它的Runnable参数应该调用showDialog()。
让我们知道它是否解决了问题。
Which thread are you calling showDialog() from? Swing components should be accessed on the Event Dispatch Thread only.
You could try SwingUtilities.invokeAndWait()
and the Runnable argument passed to it should call showDialog().
Let us know if it fixed the problem.