Home > Java > javaTutorial > body text

How to Create Multiple JavaFX Windows without Recalling launch()?

Mary-Kate Olsen
Release: 2024-10-24 06:19:01
Original
433 people have browsed it

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:

  1. Call launch() once to initialize the JavaFX runtime and create the main window.
  2. Keep the JavaFX runtime running in the background using Platform.setImplicitExit(false). This prevents JavaFX from shutting down automatically when the main window is closed.
  3. When you need to display a new window, wrap its creation and display in a Platform.runLater() block. This ensures that JavaFX operations are executed on the application thread.

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>
Copy after login

Note:

  • The Wumpus class above demonstrates the approach with a custom JavaFX application.
  • For use with Swing components, a JFXPanel can be used instead of an Application.
  • Calling Platform.exit() will terminate the JavaFX runtime, so it is important to call this method when all JavaFX operations are complete.

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!