Lukas Méndez Duus
Lukas Méndez Duus

Reputation: 436

JAVAFX: Prevent user from opening new window untill he closes the first one

I have two seperate Controllers and FXML-files. In the first controller you control the main window, here you can press a button and open a new Window.

I want to prevent the user from opening as many windows as he wants. He should only be able to open one window and close it again before he can open the next one.

This is the code that gets executed when you press the button, that I'm talking about. It opens my window perfectly, but I can still open more windows in the background which is not what I want.

I hope you can provide me some help. Thanks in advance

   @FXML
   private void addSongsToSelectedPlaylist() throws IOException {


    if (tempPlaylistName!="Library"){

        // WILL LOAD THE STAGE OF THE POPUP WINDOW AND PROVIDE THE CONSTRUCTOR THE PLAYLIST NAME
      //  AddSongController addSongController = new AddSongController();

     //   addSongController.enterSelectionMode(tempPlaylistName);


        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("addSongToPlaylistPopUp.fxml"));
            Parent root1 = (Parent) fxmlLoader.load();
            Stage stage = new Stage();
            stage.initModality(Modality.WINDOW_MODAL);
            stage.getIcons().add(new Image("sample/images/Music-icon.png"));
            stage.setResizable(false);
            stage.setAlwaysOnTop(true);

            stage.setTitle("Add songs to your playlist");
            stage.setScene(new Scene(root1));
            stage.showAndWait();

        } catch (IOException e){

            System.out.println(e.getCause());

        }

Upvotes: 3

Views: 2037

Answers (1)

Damien
Damien

Reputation: 1209

You can set the modality to APPLICATION_MODAL to prevent the application from opening any new window until the first one is closed:

   stage.initModality(Modality.APPLICATION_MODAL);

Upvotes: 3

Related Questions