Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make VBox fill the size of its parent

Tags:

javafx-2

Is this the correct way to make VBox fill its parent:

    final Group root = new Group();
    final Scene scene = new Scene(root, 1000, 800, Color.BLACK);

    final VBox c = new VBox();
    c.setAlignment(Pos.TOP_CENTER);
    c.setSpacing(10); 
    c.setFillWidth(true);
    c.getChildren().add(...);
    c.getChildren().add(...);
    c.getChildren().add(...);

    c.prefWidthProperty().bind(scene.widthProperty());
    c.prefHeightProperty().bind(scene.heightProperty());

    root.getChildren().add(c);

    stage.setTitle("blah"); //$NON-NLS-1$
    stage.setScene(scene);
    stage.show();
like image 744
cdc Avatar asked Feb 16 '12 12:02

cdc


2 Answers

You can achieve the same functionality without using bind, using only BorderPane instead of Group

final BorderPane root = new BorderPane();
// construct your VBox
root.setCenter(vbox);   
like image 158
ndrw Avatar answered Oct 05 '22 17:10

ndrw


You are making a common mistake.

It's not necessary to create a Group as the root, just directly use VBox as the root and add it to the scene.

final Scene scene = new Scene(c, 1000, 800, Color.BLACK);
like image 37
Weidian Huang Avatar answered Oct 05 '22 18:10

Weidian Huang