user5182503
user5182503

Reputation:

JavaFX: primary stage visibility and multiple independent stages

I want to create from CLI console in javafx as many stages as I want and all of them be independent from each other. I know that there can be only one instance of Application, that's why I did:

public class BaseApplication extends  Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        //we do nothing with this stage.
    }
}

The class for Stage:

public class SomeStage extends State(){
   public SomeStage(){
    ...
    show();
   }
}

And this the code which runs according to CLI commands:

Platform.runLater(()->{
   SomeStage someStage=new SomeStage();
});

However, using this code I can create only one instance of SomeStage. If in BaseApplication I do primaryStage.show(); then I can create N instances of SomeStage but only when primaryStage is visible. How to explain it and solve this dependency from primary stage?
EDIT
I found out that when primary stage is not visible and I want to create second instance of SomeStage then Platform.runLater is not called. I mean

System.out.println("Point 1");
Platform.runLater(()->{
   System.out.println("Point 2");
   SomeStage someStage=new SomeStage();
});

And I see only Point 1 on the screen and the constructor of SomeStage is not called.

Upvotes: 0

Views: 212

Answers (1)

James_D
James_D

Reputation: 209684

You haven't posted a complete example, so it's difficult to be sure what is happening, but I suspect the JavaFX toolkit is closing down when your start() method exits. Hence there is no FX Application Thread running on which to execute the runnable you supply in Platform.runLater(). Try calling

Platform.setImplicitExit(false);

in the start() method.

Upvotes: 0

Related Questions