5

This code will create a 3d scene that is 300x300 large. The viewport won't resize when I resize the containing window/stage.

Parent doesn't have any width or height properties. How do I adjust the size of the Parent and/or SubScene to the changing window size?

fho
  • 6,787
  • 26
  • 71

4 Answers4

11

bind it to the parent

SubScene subscene = new SubScene(root, 1024, 768, true, null);
subscene.setFill(Color.GREY);
subscene.setCamera(camera);
StackPane stackPane = new StackPane();
stackPane.getChildren().add(subscene);
subscene.heightProperty().bind(stackPane.heightProperty());
subscene.widthProperty().bind(stackPane.widthProperty());
MitchBroadhead
  • 811
  • 14
  • 23
6

I found it is also important to set the managed property of the SubScene object to false.

subscene.setManaged(false);

This way, the SubScene object's size will not impact the size of its parent StackPane object and resizing will also work when reducing the StackPane object's size.

pderoovere
  • 61
  • 1
  • 1
0

With the following approach you can put your SubScene into any class that extends the Pane class (e.g. BorderPane, GridPane, etc.). Also the subScene can have a different size from your Pane:

public class GuiControler extends BorderPane implements Initializable,ChangeListener {

    //Set a changeListener to the Stage's Window and Implement the ChangeListener in the class where you want to make the subScene scaling.

    stage.widthProperty().addListener(this);
    stage.heightProperty().addListener(this); 

 //....


     @Override
            public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                double width = stage.getWidth();
                double height = stage.getHeight();
                if(observable.equals(this.widthProperty())) {
                    double scale = width / 400; // 400 is my initial width size of the stage (main java app window) window
                    subScene.setWidth(200*scale); // 200 is my initial size of the subscene window
                }else if(observable.equals(this.heightProperty())){
                    double scale = height / 400; // 400 is initial size for stage height window
                    subScene.setHeight(250*scale); // 250 is initial size for subScene width
                }
            }
            }
namik
  • 53
  • 1
  • 8
-1

Ypu may want to use getTransforms().add(new Scale(parameter1, para2,...));