Jaesik Oh
Jaesik Oh

Reputation: 3

Terminate JavaFX Application using Platform.exit()

I am using JavaFX 2.2 and I have a class which extends Application. Here is my code:

Class A extends Application { 
    public void Stage(final Stage primaryStage) { ... }
    public void Start(){ 
        launch(); 
    } 
    btnLogin.setOnAction(new EventHandler<ActionEvent>() { 
        Platform.exit(); 
    } 
} 

Class B{ } 

Class C extends Application{ 
    public void Stage(final Stage primaryStage) { ... } 
    public void Start(){ 
        launch(); 
    } 
} 

Actually, Class A is login screen; it will close when I successfully log in. Then the screen closed by platform.exit() function. After that I execute view button in Class B , Class C called but there are some problems.

java.lang.IllegalStateException: Application launch must not be called more than once

I just terminate the screen by using Platform.exit() function but I can't understand why it can't be closed.

Upvotes: 0

Views: 1904

Answers (2)

ItachiUchiha
ItachiUchiha

Reputation: 36792

If Class B is the main screen and you need to Embed JavaFX in your application for Login Screen or any other screen, you don't need Class A and Class C to extend Application.

You can just create a new Window in Swing inside these classes (A and C) and use JFXPanel to embed JavaFX into your Swing Application. This way you can have full control on the application and you can easily open and close windows for Login or any other functionality that you want.

N.B. You should not have two class extending Application inside one app, as only one JavaFX thread is allowed per JVM.

Everytime you try to do this you will get this error

java.lang.IllegalStateException: Application launch must not be called more than once

Upvotes: 0

PolishCivil
PolishCivil

Reputation: 141

Platform.exit() actually terminates whole jfx. To keep things safe, just invoke launch() once and show/hide new windows.

Something like:

Platform.setImplicitExit(false);//make fx running in backgound.
Platform.runLater/AndWait {//make sure u create window in jfx thread
//window creation/show code here.
}

Upvotes: 1

Related Questions