First note:
I'm trying to monitor the resize events on a window so that I can
trigger a recalculation of the layout.
This is almost certainly the wrong approach. Layout recalculations will be triggered automatically when the stage and scene change size. If you use standard layout panes, there is no need to register listeners. If you really need a custom layout (which is highly unlikely), you should subclass Pane
and override layoutChildren()
(and other appropriate methods) to hook into the same layout system.
However, in the interests of explaining what you're observing:
You're registering an InvalidationListener
with each property, which gets notified when the property goes from a valid state to an invalid state.
The invalid state only becomes valid again if you actually request the value of the property (e.g. stage.getWidth()
). Since you never do that, the property never becomes valid, and hence never goes from valid to invalid again.
Instead, register a ChangeListener
with each property:
stage.widthProperty().addListener((observable, oldWidth, newWidth) -> {
System.out.println("Width changed");
});
stage.heightProperty().addListener((observable, oldHeight, newHeight) -> {
System.out.println("Height changed");
});
Alternatively, you can force validation by requesting the value (though I think the change listener above actually gets to the point of what you are trying to do):
stage.widthProperty().addListener(observable -> {
System.out.println("Width changed: "+stage.getWidth());
});
etc.