javafx GridPane Layout

0

GridPane Layout : A javaFX layout

  • In javaFX GridPane layout it position the layout in rows and columns.
  • Means it divides the area into grids.
  • The GridPane, commonly used in business applications.

An example of GridPane 2 columns and 3 rows :


package javafxtuts;


import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

/**
 *
 * @author javafxtuts
 */
public class Javafxtuts extends Application {
    
    @Override
    
  public void start(Stage primaryStage) {
    primaryStage.setTitle("JavaFXtuts");
    GridPane root = new GridPane();
    root.setPadding(new Insets(5));
       
    Label fNameLbl = new Label("First Name");
TextField fNameFld = new TextField();
Label lNameLbl = new Label("Last Name");
TextField lNameFld = new TextField();
Button saveButton = new Button("Save");
// First name label
GridPane.setHalignment(fNameLbl, HPos.RIGHT);
root.add(fNameLbl, 0, 0);
// Last name label
GridPane.setHalignment(lNameLbl, HPos.RIGHT);
root.add(lNameLbl, 0, 1);
// First name field
GridPane.setHalignment(fNameFld, HPos.LEFT);
root.add(fNameFld, 1, 0);
// Last name field
GridPane.setHalignment(lNameFld, HPos.LEFT);
root.add(lNameFld, 1, 1);
// Save button
GridPane.setHalignment(saveButton, HPos.RIGHT);
root.add(saveButton, 1, 2);
    Scene scene = new Scene(root, 300, 300);
    primaryStage.setScene(scene);
    primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

Output :

Gridpane example Explaination :

    • It is an example in which 2 columns and 3 rows.
    • To use GridPane Layout we create an object of GridPane Layout AS

GridPane root = new GridPane();

  • We use setHalignment(lNameFld, HPos.LEFT) to set horizontal alignment and using HPos.LEFT to put the node in left side.
Share.

Leave A Reply