James Gosling: idealism, the Internet and Java, Pt I

Friday 15 May 2015

JavaFX: arranging components on GridPane

JavaFX provides different layout panes for arranging components on them. for example GridPane, BorderPane etc.
This article describes about arranging components on GridPane. GridPane layout manager allows arranging components/controls using overloaded method. ie add.

                       1. add(controlinstance, colindex, rowindex)

                      EX:  GridPane pane = new GridPane();
                               pane.add(new Separator(), 0, 0);

the above example code puts the separator on 0th row and 0th column of a gridPane.

                     2. add(Node controlinstance,int colIndex,int rowIndex,int colSpan,int rowSpan)   
                     EX: GridPane pane = new GridPane();
                             pane.add(new Separator(),0 , 0, 2, 0);

Following code describes the GridPane with a login screen.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import com.atmecs.utilities.Validations;

public class Main extends Application {

Button buttonLogin;
Button buttonCancel;
TextField textFieldUsername;
PasswordField PasswordFieldPassword;

public void start(Stage primaryStage) {



private void initComponents(Stage primaryStage) {
GridPane gridPane = new GridPane();

gridPane.setPadding(new Insets(25, 25, 25, 25));

Scene scene = new Scene(gridPane, 320, 230);

Text title = new Text("Login");
title.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));
gridPane.add(title, 0, 0, 2, 1);

Label labelUsername = new Label("Username:");
gridPane.add(labelUsername, 0, 1);
textFieldUsername = new TextField();
gridPane.add(textFieldUsername, 1, 1);

Label labelPassword = new Label("Password:");
gridPane.add(labelPassword, 0, 2);
PasswordFieldPassword = new PasswordField();
gridPane.add(PasswordFieldPassword, 1, 2);

buttonLogin = new Button("Login");

buttonCancel = new Button("Cancel");

gridPane.add(new Separator(), 0, 4, 2, 1);
HBox hbBtn = new HBox(10);

gridPane.add(hbBtn, 1, 5);

        public static void main(String[] args) {



No comments:

Post a Comment

Popular posts

Demonstration of Java NullPointerException

NullPointerException causes and reasons Since Java is object oriented programming language, every thing is considered to be an object. and t...