This question is rather simple: Is it possible to receive resize events that only trigger once, even if width
and height
change at the same time?
I have an application that calculates an image in the size of the window pixel per pixel. When the window resizes the image is being calculated again. The problem is, that when listening to the widthProperty()
and heightProperty()
there will always be two events that fire, even if width
and height
changed in the same loop cycle. This results in one redundant calculation. Is there a way to listen for resizes once per update?
A simple example:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Group root = new Group();
primaryStage.setScene(new Scene(root));
primaryStage.show();
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.widthProperty().addListener((observable, oldValue, newValue) ->
System.out.println("old: (" + oldValue + ", " + primaryStage.getHeight() + "); "
+ "new: (" + newValue + ", " + primaryStage.getHeight() + ")")
);
primaryStage.heightProperty().addListener((observable, oldValue, newValue) ->
System.out.println("old: (" + primaryStage.getWidth() + ", " + oldValue + "); "
+ "new: (" + primaryStage.getWidth() + ", " + newValue + ")")
);
primaryStage.setWidth(400);
primaryStage.setHeight(400);
}
}
This prints:
old: (800.0, 800.0); new: (400.0, 800.0)
old: (400.0, 800.0); new: (400.0, 400.0)
But I want this as an output only:
old: (800.0, 800.0); new: (400.0, 400.0)