0

Since I can't use Scene builder to set this method

I'm trying to get the Window of the scene that is now displayed and set the method

public class MainController {
@FXML
    private Button Settings;
    public void initialize() {
     
    Window w =Settings.getScene().getWindow();

    w.setOnCloseRequest(ac->{

            System.out.println("Done");
        });
   }
}

Then I have noticed that w = null ... and the get Scene method returns null too..

so 1) is there any way to make this with Scene builder without using (initialize way) 2) how can i fix the code above so i can use the window ?

  • I recommend reading the [this answer](https://stackoverflow.com/a/13247005/6395627) and [this answer](https://stackoverflow.com/a/30910015/6395627) and choose which solution best fits your needs. – Slaw Jun 22 '20 at 01:29

1 Answers1

1

Don't use getScene() in initialize method since you will get null. The root element is still not placed on the scene when initialize method is called for the controller.

Try this:

ChangeListener<Window> windowListener = new ChangeListener<>() {
    @Override
    public void changed(ObservableValue obs, Window oldVal, Window newVal) {
        if (newVal != null) {
            setting.getScene().getWindow().setOnCloseRequest(eh -> System.out.println("Done"));
            setting.getScene().windowProperty().removeListener(this);
        }
    }
};

ChangeListener<Scene> sceneListener = new ChangeListener<>() {
    @Override
    public void changed(ObservableValue obs, Scene oldVal, Scene newVal) {
        if (newVal != null) {
            newVal.windowProperty().addListener(windowListener);
            setting.sceneProperty().removeListener(this);
        }
    }
};

setting.sceneProperty().addListener(sceneListener);

In order to avoid unused listeners, I added a listener removal after setOnCloseRequest is set.

Oboe
  • 2,643
  • 2
  • 9
  • 17
  • Ok newVal !=null now but the getWindow returns null .... still not working – Wissam Sbenaty Jun 22 '20 at 00:06
  • ``` Settings.sceneProperty().addListener((obs, oldVal, newVal) -> { if (newVal != null) { newVal.windowProperty().addListener((obs2,last,now)->{ if(now!=null) now.setOnCloseRequest(ac->{ System.out.println("Done"); }); else System.out.println("Null"); } ); } }); ``` I used the same idea to check if the Window is null or not.... it Works perfectly ! – Wissam Sbenaty Jun 22 '20 at 00:16
  • The listener approach shown by @Wissam is the proper way to be notified of when the `Window` is no longer null. Adding a button which, _when manually pressed by the end user(!!!)_, adds a handler to the host window is not user friendly. – Slaw Jun 22 '20 at 00:55
  • @Slaw Right ... so i can't use Oboe's code ....doesn't help me to set the method – Wissam Sbenaty Jun 22 '20 at 01:09
  • @Slaw, you are right. The listener was my initial answer. My mistake for the wrong update... Guess is sunday night ;). I'll undo the answer back to my initial answer. – Oboe Jun 22 '20 at 01:25
  • @Slaw, since you will use the listeners only once, I would remove them after they are used. I updated the code to include that. – Oboe Jun 22 '20 at 01:48