While converting the PageFlow mode from Java3D to JavaFX in our Java PDF Viewer, one of the things that I found unclear was how to listen out for window resizes.
In awt, you are able to add a ComponentListener to the JPanel, and then override the componentResized() method to get the new width and height like so:
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
System.out.println("New width: " + getWidth() + " New height: " + getHeight());
}
});
Unfortunately, neither the JavaFX Stage or Scene has any such listener you can use.
My next thought, having just added EventHandlers for mouse events to the scene, was that I would be able to add an EventHandler for a scene resize to the scene. But, unfortunately there is no such event to listen to.
scene.setOnMousePressed(EventHandler<MouseEvent>);// Adding a MouseEvent listener
scene.setOnWindowResize(EventHandler<WindowListener>);// Does not exist :(
The correct way to listen for window resizes is to add a ChangeListener to the width and height properties of the scene, like so:
scene.widthProperty().addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneWidth, Number newSceneWidth) {
System.out.println("Width: " + newSceneWidth);
}
});
scene.heightProperty().addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight, Number newSceneHeight) {
System.out.println("Height: " + newSceneHeight);
}
});
If you are using a JFXPanel, you could add a ComponentListener in the same way you would to a JPanel, but if you are modifying things on the scene, it would be preferable to use the method above to simplify things by keeping the listener on the correct thread.
I have written several articles on converting our Java3D usage into JavaFX and you can read the other articles here.
Our software libraries allow you to
Convert PDF files to HTML |
Use PDF Forms in a web browser |
Convert PDF Documents to an image |
Work with PDF Documents in Java |
Read and write HEIC and other Image formats in Java |
It’s wonderful blog 🙂 but If i want to re size my stage on button click how can i do that?
The Stage object has setWidth and setHeight methods, so you would add an EventHandler to the button that calls those methods.
E.g.
How can you make stage resizable over the current width or height but at certain size you can’t resize under certain size i.e
stage is 300, 300 but you cant make 299, 300, or 300, 299
Hi Ahmed, something like this should enable you to make the stage smaller:
this was what i wanted, i solved it, cant resize under 140, or over 500, thanks by the way
public void start(Stage primaryStage) throws Exception {
Button button=new Button(“Resize me”);
StackPane sp=new StackPane();
sp.getChildren().add(button);
Scene scene=new Scene(sp);
primaryStage.setMinWidth(140);
primaryStage.setMinHeight(140);
primaryStage.setMaxWidth(500);
primaryStage.setMaxHeight(500);
primaryStage.setWidth(100);
primaryStage.setHeight(100);
primaryStage.setScene(scene);
primaryStage.show();
Hi how to handle proportional resizing of stage ? is there any way that if I resize the window from width side stage should resized by both height and width side proportionally ?
This works fine. Thanks.