Leon Atherton Leon is a developer at IDRsolutions and product manager for BuildVu. He oversees the BuildVu product strategy and roadmap in addition to spending lots of time writing code.

How to add a window resize listener to JavaFX scene

1 min read

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
Leon Atherton Leon is a developer at IDRsolutions and product manager for BuildVu. He oversees the BuildVu product strategy and roadmap in addition to spending lots of time writing code.

How to enable Text to Speech in JavaFX and…

As a developer at IDR Solutions I spend a lot of my time working on the JPedal Java PDF library and lately I have...
Sylwia Dorota Kedzia
59 sec read

7 Replies to “How to add a window resize listener to JavaFX…”

  1. It’s wonderful blog 🙂 but If i want to re size my stage on button click how can i do that?

    1. The Stage object has setWidth and setHeight methods, so you would add an EventHandler to the button that calls those methods.

      E.g.

          @Override
          public void start(Stage primaryStage) {
              Button btn = new Button();
              btn.setText("Resize Stage");
              btn.setOnAction(new EventHandler() {
                  
                  @Override
                  public void handle(ActionEvent event) {
                      primaryStage.setWidth(600);
                      primaryStage.setHeight(500);
                  }
              });
              
              StackPane root = new StackPane();
              root.getChildren().add(btn);
              
              Scene scene = new Scene(root, 300, 250);
              
              primaryStage.setTitle("Hello World!");
              primaryStage.setScene(scene);
              primaryStage.show();
          }
  2. 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

    1. Hi Ahmed, something like this should enable you to make the stage smaller:

          @Override
          public void start(Stage primaryStage) {
              primaryStage.setWidth(300);
              primaryStage.setHeight(300);
              Button btn = new Button();
              btn.setText("Resize stage");
              btn.setOnAction(new EventHandler() {
                  
                  @Override
                  public void handle(ActionEvent event) {
                      primaryStage.setWidth(300);
                      primaryStage.setHeight(299);
                  }
              });
              
              StackPane root = new StackPane();
              root.getChildren().add(btn);
              
              Scene scene = new Scene(root, 300, 250);
              
              primaryStage.setTitle("Hello World!");
              primaryStage.setScene(scene);
              primaryStage.show();
          }
      1. 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();

  3. 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 ?

Comments are closed.