2 Replies Latest reply: May 18, 2013 3:16 PM by 990211 RSS

    Region.setWidth/Height not working

      i am using the following code to resize/fullscreen the application
      private static class SceneSizeChangeListener implements ChangeListener<Number> {
           private Scene s;
           private double initHeight;
           private double initWidth;
           public SceneSizeChangeListener(final Scene s){
                this.s = s;
                this.initHeight = 720;
                this.initWidth = 1080;
                s.rootProperty().addListener(new ChangeListener<Parent>(){
                     @Override public void changed(ObservableValue<? extends Parent> arg0, Parent arg1, Parent arg2) {
           @Override public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue){
           private void scale(){
                double newWidth  = s.getWidth();
                double newHeight = s.getHeight();
                double widthFactor = newWidth/initWidth;
                double heightFactor = newHeight/initHeight;
                Scale scale = new Scale(widthFactor, heightFactor, 0, 0);
                //not working
                ((Region)s.getRoot()).setPrefSize(newWidth, newHeight);
                ((Region)s.getRoot()).setMinSize(newWidth, newHeight);
                ((Region)s.getRoot()).setMaxSize(newWidth, newHeight);
                //not working
                //this deforms everything, so is not an option
                for(Node n : ((Pane)s.getRoot()).getChildren())
      but the set width/height are not working so the root (a Pane) shows smaller than the window when the window is smaller than the original and viceversa

      the code is made so that the elements of the window grows/shrinks accordingly to the window size (and is normal that the root grows/shrinks, since is made to do it too), but then when i try to modify the width/height it does not do anything
        • 1. Re: Region.setWidth/Height not working
          If I understand correctly, you're trying to get an effect whereby resizing the scene (by resizing the window) results in zooming in or out on the content.

          You shouldn't need to change the size of the root; if the root of the Scene is resizable (e.g. a Region or Control), its size will track the size of the Scene. If it's a Group, it will be clipped.

          The following works for me; I just used a couple of bindings to manage the scaling.
          import javafx.application.Application;
          import javafx.scene.Scene;
          import javafx.scene.layout.Pane;
          import javafx.scene.paint.Color;
          import javafx.scene.shape.Circle;
          import javafx.scene.shape.Rectangle;
          import javafx.scene.transform.Scale;
          import javafx.stage.Stage;
          public class ResizingScreen extends Application {
            public void start(Stage primaryStage) {
              final Pane root = new Pane();
              final Rectangle rect = new Rectangle(50, 50, 150, 150);
              final Circle circ = new Circle(150, 150, 100);
              circ.setFill(Color.CORNFLOWERBLUE.deriveColor(0, 1, 1, 0.5));
              root.getChildren().addAll(rect, circ);
              final int initialWidth = 600;
              final int initialHeight = 400;
              Scale scale = new Scale(1, 1);
              Scene scene = new Scene(root, initialWidth, initialHeight);
            public static void main(String[] args) {
          • 2. Re: Region.setWidth/Height not working
            more compact code, but still happens to do exactly the same (and have to tweak it so it works after scene.setRoot();)

            here are some images on what happens (turned the background to blue so easier to see where the root element ends on the scene, and i'd like to have it to cover all since the elements r already now smaller)

            EDIT: after some testings have been able to make it work if instead of:
            final Pane root = new Controller();
            final Pane root = new Pane();
            final Pane controller = new Controller();
            don't understand the reason, but it works... (then seems i will have to change all the Scene.setRoot() or adding a listener so instead of changing the root goes and changes the children...)

            Edited by: Hello World! on May 18, 2013 1:10 PM