JavaFX中的JOptionPane使窗口“不响应"
我正在构建JavaFX应用程序,并且正在使用JOptionPane来显示对话框
I am building a JavaFX app and am using JOptionPane to display dialogs
我遇到的问题之一是创建新对话框并在5秒钟左右的时间内不关闭它会导致JavaFX主阶段进入不响应"状态
One of the problems I've encountered is that creating a new dialog and not dismissing it within 5 or so seconds will cause the main JavaFX stage go into a 'Not Responding' state
在新线程中运行joptionpane代码是可行的,但会导致对话框不是模态的,这不适合我正在构建的应用程序
Running the joptionpane code in a new thread works, but causes the dialog not to be modal, which is not suitable for the app i'm building
这一切都在Windows中运行.
This is all running in Windows.
任何解决不响应问题的方法将不胜感激:)
Any ways to fix the not responding problem would be greatly appreciated :)
我使用的代码是(来自JavaFX主线程)
JOptionPane.showMessageDialog(null, "message here");
可能是null导致了问题?
The code I use is (from the main JavaFX thread)
JOptionPane.showMessageDialog(null, "message here");
Perhaps null is causing the problem?
您不能仅从FX线程显示Jwing组件,它是Swing组件.您有三种选择:
You can't just show a JoptionPane, which is a Swing component, from the FX thread. You have three options:
- 创建一个Swing应用程序并将所有fx节点放在 或使用外部库,例如 ControlsFX (仅JavaFX 8,还有其他可用于JavaFX 2的库)
- 将您的JOptionPane放在 SwingNode (仅限JavaFX 8)
- create a Swing application and place all the fx nodes in JFXPanels.
- use FX nodes only, for example by using a modal stage or by using an external library such as ControlsFX (JavaFX 8 only but there are other libraries available that work with JavaFX 2)
- place your JOptionPane in a SwingNode (JavaFX 8 only)
对于1和3,您需要小心在Swing EDT线程和JavaFX线程上的JavaFX节点上创建/修改Swing组件(除非您将两者与Java 8中可用的新JVM选项合并).而且我不确定100%如果将Swing和JavaFX混合使用,那么模态周如何工作.
For 1 and 3 you need to be careful to create/modify the Swing components on the Swing EDT thread and the JavaFX nodes on the JavaFX thread (unless you merge the two with the new JVM option available in Java 8). And I'm not 100% sure how the modality week work if you mix Swing and JavaFX.
如果您正在创建JavaFX应用程序,则没有理由不选择2.
If you are creating a JavaFX application there is little reason not to choose 2.