Cannot set style once stage has been set visible


Question

i have a stage and set it's style to

stage.initStyle(StageStyle.TRANSPARENT);

after some seconds i need to change initStyle to Decorate but when i use

stage.initStyle(StageStyle.Decorate );

give me this exception

java.lang.IllegalStateException: Cannot set style once stage has been set visible

any ideas?

1
3
5/6/2014 7:52:31 PM

Accepted Answer

What Not to Do

As the exception says, you

Cannot set style once stage has been set visible

So, if you can't do that, don't try to do it.

What to Do

Instead, hide your transparent stage and create a new stage with the new style. When doing so, be careful because the default behaviour is to shut down the JavaFX system once all stages have been hidden. To avoid this either switch off the default behaviour and explicitly shutdown the JavaFX system when needed or ensure that you always have at least one Window visible.

Example

// initialize your splash stage.
Platform.setImplicitExit(false);
splashStage.initStyle(StageStyle.TRANSPARENT);
. . .
// create your main stage.
Stage mainStage = new Stage();
mainStage.setScene(mainScene);
mainStage.initStyle(StageStyle.DECORATED); 
mainStage.setOnHide(event -> Platform.exit())
. . .
// on some later event hide your splash stage and show your main stage.
splashStage.hide();
mainStage.show();  

Related

Here is a full executable sample based on an answer to a previous question => How to create Splash screen with transparent background in JavaFX.

7
5/23/2017 11:54:11 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon