5

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 Answers1

12

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.

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