0

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?

SuperMrBlob
  • 611
  • 6
  • 19
  • 1
    Maybe you should go for [this](http://edu.makery.ch/blog/2012/10/30/javafx-2-dialogs/)? – Marko Topolnik Jan 14 '14 at 08:42
  • I've thoroughly experimented with these dialogs, and they provided more problems then solutions. The size of the dialogs (bigger than my stage) and the way they pop up on screen are the two main ones. I suppose this seems nitpicky, but I believe that the way an app looks is crucial to its success. – SuperMrBlob Jan 14 '14 at 09:03
  • And [this](http://tech.chitgoks.com/2013/06/19/how-to-create-alert-dialog-like-joptionpane-in-java-fx-2/) one? It looks a lot nicer. – Marko Topolnik Jan 14 '14 at 09:05
  • 1
    @MarkoTopolnik in that case [controlsFX](http://fxexperience.com/controlsfx/features/) is probably better (JavaFX 8). – assylias Jan 14 '14 at 09:28
  • Why do you use a swing component instead of [a modal stage](http://stackoverflow.com/questions/10486731/how-to-create-a-modal-window-in-javafx-2-1) for example? Do you create/show the JOptionPane on the swing thread? Can you show some code? Etc. – assylias Jan 14 '14 at 09:30
  • The code I use is (from the main JavaFX thread) `JOptionPane.showMessageDialog(null, "message here");` Perhaps null is causing the problem? – SuperMrBlob Jan 15 '14 at 05:24
  • Here is another library you could try: [MessageBox](http://sourceforge.jp/projects/jfxmessagebox/) – Jurgen Jan 15 '14 at 07:10

2 Answers2

2

You can't just show a JoptionPane, which is a Swing component, from the FX thread. You have three options:

  1. create a Swing application and place all the fx nodes in JFXPanels.
  2. 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)
  3. 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.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • Thanks for the rundown. Seems I'll have to use Java 8 then :P – SuperMrBlob Jan 16 '14 at 06:24
  • The modal stage option in 2 works with FX 2.x. But if you can use java 8 then it is a great step up (and now only 2 months away from official release!). – assylias Jan 16 '14 at 07:59
0

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:

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406