rak
rak

Reputation: 147

How to set vbox size to window size in javafx?

I am using JavaFX for UI.How to set Vbox layout's size to window size? I tried the below code,but couldnot view the components added in vbox.

    VBox vbox = new VBox();
    vbox.setPadding(new Insets(10, 10, 10, 10));  
    vbox.setSpacing(10);

Upvotes: 2

Views: 25552

Answers (3)

Mateusz
Mateusz

Reputation: 111

This sets VBOX on 80% of the Stage width:

Stage window = PrimaryStage;
VBox layout = new VBox(10);
//multiply to set size (0.80 is like 80% of the window)
layout.prefWidthProperty().bind(window.widthProperty().multiply(0.80));

Upvotes: 11

G. Kaspárov
G. Kaspárov

Reputation: 9

You can use:

VBox vbox = new VBox();
vbox.setPrefWidth(400);// prefWidth
vbox.setPrefHeight(500);// prefHeight

or

VBox vbox = new VBox();
vbox.setPrefSize(400, 500);// prefWidth, prefHeight

Read more here!

Upvotes: -6

pmoule
pmoule

Reputation: 4352

  1. To set your vBox size to the main window's size just add it to the scene primaryStage.setScene(new Scene(vBox, 200, 200));
  2. Add nodes to your VBox to show them inside the box
    vBox.getChildren().add(label);

Try this example:

package javafxapplication1;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JavaFXApplication1 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        VBox vBox = new VBox();
        vBox.setStyle("-fx-background-color: #ABABAB");

        Label label = new Label("Test");

        vBox.getChildren().add(label); //Add new node to vBox

        primaryStage.setScene(new Scene(vBox, 200, 200)); //Add vbox to scene
        primaryStage.show();
    }
}

Upvotes: -3

Related Questions