DrExpresso
DrExpresso

Reputation: 73

Have scene and stage interactive at the same time in JavaFX

Hi so i am trying to create a application however if i open up a scene on top a stage i cannot interact with both of the windows, only the one on top. After researching i am guessing this can't be done and i would have to modify the code to separate the big MainApp into smaller classes and use Platform.runLater and threads in one main class to load each scene and stage on a separate thread. I have tried this and have had no success.

public class MainApp extends Application {

private Stage primaryStage;
private BorderPane rootLayout;


@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("AddressApp");

    // Set the application icon.
    this.primaryStage.getIcons().add(new Image("file:resources/images/address_book_32.png"));

    initRootLayout();

    showPersonOverview();
}

/**
 * Initializes the root layout and tries to load the last opened
 * person file.
 */
public void initRootLayout() {
    try {
        // Load root layout from fxml file.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class
                .getResource("view/RootLayout.fxml"));
        rootLayout = (BorderPane) loader.load();

        // Show the scene containing the root layout.
        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);

        // Give the controller access to the main app.
        RootLayoutController controller = loader.getController();
        controller.setMainApp(this);

        primaryStage.show();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Try to load last opened person file.
    File file = getPersonFilePath();
    if (file != null) {
        loadPersonDataFromFile(file);
    }
}

/**
 * Opens a dialog to show birthday statistics.
 */
public void showBirthdayStatistics() {
    try {
        // Load the fxml file and create a new stage for the popup.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(MainApp.class.getResource("view/BirthdayStatistics.fxml"));
        AnchorPane page = (AnchorPane) loader.load();
        Stage dialogStage = new Stage();
        dialogStage.setTitle("Birthday Statistics");
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.initOwner(primaryStage);
        Scene scene = new Scene(page);
        dialogStage.setScene(scene);

        // Set the dialog icon.
        dialogStage.getIcons().add(new Image("file:resources/images/calendar.png"));

        // Set the persons into the controller.
        BirthdayStatisticsController controller = loader.getController();
        controller.setPersonData(personData);

        dialogStage.show();

    } catch (IOException e) {
        e.printStackTrace();
    }
}



/**
 * Returns the main stage.
 * @return
 */
public Stage getPrimaryStage() {
    return primaryStage;
}

public static void main(String[] args) {
    launch(args);
}
}

enter image description here

In the above image i can only interact with the Birthday statics scene and not the primary stage.

Upvotes: 1

Views: 126

Answers (1)

NielsNet
NielsNet

Reputation: 828

The line :

dialogStage.initModality(Modality.WINDOW_MODAL);

Makes your new window a Modal that blocks any events for other windows.

You should replace it with:

dialogStage.initModality(Modality.NONE);

You can read more here

Upvotes: 1

Related Questions