JavaFX How to - Set a CSS for the GridPane








Question

We would like to know how to set a CSS for the GridPane.

Answer

/* w w w  .j ava  2  s . c o m*/
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        TextField fNameFld = new TextField();
        Label fNameLbl = new Label("First Name:");

        TextField lNameFld = new TextField();
        Label lNameLbl = new Label("Last Name:");

        GridPane root = new GridPane();
        root.addRow(0, fNameLbl, fNameFld);
        root.addRow(1, lNameLbl, lNameFld);
        
        // Set a CSS for the GridPane
        root.setStyle("-fx-padding: 10;" + 
                      "-fx-border-style: solid inside;" + 
                      "-fx-border-width: 2;" +
                      "-fx-border-insets: 5;" + 
                      "-fx-border-radius: 5;" + 
                      "-fx-border-color: blue;");

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
}