Search code examples
javajavafxwindowstage

JavaFX detect changes of Stage screen location


Is there a way in JavaFX to detect when the Stage/Window position is changed relative to the screen?

I can detect when resizing the Stage but not when the Stage is relocated.


Solution

  • You can use the xProperty and yProperty of Window.

    The horizontal location of this Stage on the screen. Changing this attribute will move the Stage horizontally. Changing this attribute will not visually affect a Stage while fullScreen is true, but will be honored by the Stage once fullScreen becomes false.

    stage.xProperty().addListener((obs, oldVal, newVal) -> System.out.println("X: " + newVal));
    stage.yProperty().addListener((obs, oldVal, newVal) -> System.out.println("Y: " + newVal));