问题
I am building a JavaFX app and am using JOptionPane to display dialogs
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
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
This is all running in Windows.
Any ways to fix the not responding problem would be greatly appreciated :)
EDIT: The code I use is (from the main JavaFX thread)
JOptionPane.showMessageDialog(null, "message here");
Perhaps null is causing the problem?
回答1:
You can't just show a JoptionPane, which is a Swing component, from the FX thread. You have three options:
- 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)
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.
If you are creating a JavaFX application there is little reason not to choose 2.
回答2:
Don't use JOptionPane in a JavaFX application, use a JavaFX Alert instead. Alert was introduced in Java 8u40.
Using JOptionPane in JavaFX may be problematic due to the reasons pointed out in assylias's answer.
See also:
- No errors, but crashing - JOptionPane - JavaFX
来源:https://stackoverflow.com/questions/21108927/joptionpane-in-javafx-making-window-not-respond