How to Call launch() More Than Once in Java
The JavaFX application launch method, launch(), is designed to be called only once per application. Attempting to call launch() more than once results in an "IllegalStateException" error.
Solution: Wrap Subsequent Window Creation in Platform.runLater()
Instead of calling launch() multiple times, consider the following approach:
Example Code:
public class Wumpus extends Application {
private static final Insets SAFETY_ZONE = new Insets(10);
private Label cowerInFear = new Label();
private Stage mainStage;
@Override
public void start(final Stage stage) {
mainStage = stage;
mainStage.setAlwaysOnTop(true);
Platform.setImplicitExit(false);
cowerInFear.setPadding(SAFETY_ZONE);
cowerInFear.setOnMouseClicked(event -> Platform.exit());
stage.setScene(new Scene(cowerInFear));
}
public static void main(String[] args) {
launch(args);
// Another window can be created here by wrapping its creation
// and display in a Platform.runLater() block.
Platform.runLater(() -> {
Stage newStage = new Stage();
newStage.setScene(new Scene(new Label("Another Window")));
newStage.show();
});
}
}
Note:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3