Valentin
Valentin

Reputation: 280

Make Objects of Gridpane fill screen

I am currently trying to learn JavaFX. I have created a small setup with a Gridpane shown in the screenshot below

enter image description here

I made the background of the gridpane red so as you can see it fills the whole screen. But unfortunately the labels and textfields stay the same size. I want them to scale with the size of the gridpane.

Below is the code at the moment:

   @Override
public void start(Stage primaryStage) throws Exception{
    primaryStage.setTitle("Test Application");

    grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(10, 10, 10, 10));
    grid.setPrefSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    grid.setMinSize(WINDOW_WIDTH, WINDOW_HEIGHT);

    textWelcome = new Text("Welcome");
    textWelcome.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
    grid.add(textWelcome, 0, 0, 2, 1);

    labelName = new Label("User Name: ");
    grid.add(labelName, 0,1);

    nameField = new TextField();
    grid.add(nameField, 1,1);

    labelPW = new Label("Password: ");
    grid.add(labelPW,0,2);

    pwField = new PasswordField();
    grid.add(pwField,1,2);

    buttonSign = new Button("Sign in");
    HBox hBox = new HBox(10);
    hBox.setAlignment(Pos.BOTTOM_RIGHT);
    hBox.getChildren().add(buttonSign);
    grid.add(hBox,1,3);

    grid.setStyle("-fx-background-color:red");
    //grid.setGridLinesVisible(true);
    scene = new Scene(grid, WINDOW_WIDTH, WINDOW_HEIGHT);
    primaryStage.setScene(scene);
    primaryStage.show();
}

So now i want to know on how to make the whole thing scale up so that there is not a big margin at the sides.

Upvotes: 2

Views: 2278

Answers (1)

James_D
James_D

Reputation: 209724

You can use ColumnConstraints and RowConstraints to configure the behavior of the columns and rows. You can also set properties on individual nodes using the static GridPane methods.

See the sections entitled "Row/Column Sizing" and "Optional Layout Constraints" in the documentation

Here is a simple example:

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class GridPaneColumnGrowExample extends Application {

    private static final double WINDOW_WIDTH = 600 ;
    private static final double WINDOW_HEIGHT = 400 ;

    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("Test Application");

        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(10, 10, 10, 10));
        grid.setPrefSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        grid.setMinSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        ColumnConstraints leftCol = new ColumnConstraints();
        leftCol.setHalignment(HPos.RIGHT);
        leftCol.setHgrow(Priority.NEVER);

        ColumnConstraints rightCol = new ColumnConstraints();
        rightCol.setHgrow(Priority.ALWAYS);

        grid.getColumnConstraints().addAll(leftCol, rightCol);

        Text textWelcome = new Text("Welcome");
        textWelcome.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
        grid.add(textWelcome, 0, 0, 2, 1);

        // override column constraints alignment for this cell
        GridPane.setHalignment(textWelcome, HPos.LEFT);

        Label labelName = new Label("User Name: ");
        grid.add(labelName, 0,1);

        TextField nameField = new TextField();
        grid.add(nameField, 1,1);

        Label labelPW = new Label("Password: ");
        grid.add(labelPW,0,2);

        PasswordField pwField = new PasswordField();
        grid.add(pwField,1,2);

        Button buttonSign = new Button("Sign in");
        grid.add(buttonSign,1,3);
        GridPane.setHalignment(buttonSign, HPos.RIGHT);

        grid.setStyle("-fx-background-color:red");
        //grid.setGridLinesVisible(true);
        Scene scene = new Scene(grid, WINDOW_WIDTH, WINDOW_HEIGHT);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

Upvotes: 2

Related Questions