Home >Java >javaTutorial >How to Create Multiple JavaFX Windows without Recalling launch()?
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:
<code class="java">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(); }); } }</code>
Note:
The above is the detailed content of How to Create Multiple JavaFX Windows without Recalling launch()?. For more information, please follow other related articles on the PHP Chinese website!