2

I use JavaFX for a GUI that waits for some user's input and tries to process that input. In case of any exception a javafx.scene.control.Alert is opened to present the exception to the user. The call for that Alert is done in the method that handles the exception.

This method is supposed to be tested: For a special input, check if the right exception is thrown. The right exception is actually thrown and the handling method is called, but now here's the problem: In the test case there is no real Application running and no Stage created. Apparently because of that the creation of the Alert-dialog throws the exception:

java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:550)
at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:512)
at javafx.scene.control.Control.<clinit>(Control.java:87)
... 46 more

That causes the test to fail because now another incorrect exception is thrown.

So: Is there any way to show the Alert without having a Stage?

Pidro
  • 143
  • 2
  • 7
  • 1
    I'm pretty sure you can call a alert without having a stage. Can you provide some of your code? I expect it to be somewhat like this: Can u provide some code? I think u have something like this? } catch (Exception e) { Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Error title here"); alert.setContentText("some content"); alert.show(); } – Jouke May 16 '17 at 18:31
  • @Jouke please don't post code in the comments. No sense can be made out of them, and they disturb the eyes. – dumbPotato21 May 16 '17 at 18:32
  • 3
    This has nothing to do with whether or not a `Stage` is displayed: the error indicates the JavaFX toolkit has not been initialized. `Application.launch()` needs to have been invoked for this to happen (there is a workaround you can use where you instantiate a `JFXPanel`, but this feels a bit fragile). My answer to [this question](http://stackoverflow.com/questions/32739199/javafx-software-design) (which is an entirely different question) has a mechanism for starting an FX Application which essentially does nothing other than start the FX toolkit. – James_D May 16 '17 at 18:35

0 Answers0