Creating A Registration Form in JavaFX - CalliCoder
Creating A Registration Form in JavaFX - CalliCoder
form in JavaFX
Enter your email..
SUBSCRIBE →
This is the second post of my JavaFX tutorial series. In the previous post, we created a simple Hello
World application and learned about JavaFX application lifecyle.
Stay in touch
If you are a beginner in JavaFX and want to understand the basics, I encourage you to go through my
previous post before reading this one. TWITTER
In this post, We’ll create an interactive javafx application with multiple UI controls.
GITHUB
The application will have a registration form where users can enter their details and then submit the
LINKEDIN
form. When the user submits the form, we’ll greet him by showing an alert box.
Here is a screenshot of the application that we are going to build in this tutorial - FACEBOOK
RECENT ARTICLES
Deploying a full-stack
Spring boot, Mysql, and
React app on…
by RAJEEV SINGH · 3 MINS
8 Software engineering
principles to live by
by RAJEEV SINGH · 5 MINS
Deploying a stateless Go
app with Redis on
Kubernetes
by RAJEEV SINGH · 2 MINS
The rst thing you need to do is to create the Main class for your JavaFX application which extends Stock Span Problem
javafx.application.Application , and override it’s start() method. by RAJEEV SINGH · 1 MINS
Open your favorite IDE and create a new Java project along with a class named
RegistrationFormApplication with the following contents -
POPULAR ARTICLES
launch(args);
}
Spring Boot, MySQL, JPA,
} Hibernate Restful CRUD
API Tutorial
by RAJEEV SINGH · 7 MINS
// Position the pane at the center of the screen, both vertically and horizontally
gridPane.setAlignment(Pos.CENTER);
gridPane.getColumnConstraints().addAll(columnOneConstraints, columnTwoConstrains);
return gridPane;
}
We rst instantiate a new GridPane, and then set the alignment and padding properties. We also add
gaps between columns and rows of the grid pane by setting Hgap and Vgap properties.
I have also applied some column constraints to the grid pane. The ColumnConstraints constructor
takes the min width , preferred width and max width values.
All the nodes in column one will have a min-width of 100, pref-width of 100 and max-width of
Double.MAX_VALUE .
Similarly, all the nodes in column two will have a min-width of 200, pref-width of 200 and max-width
of Double.MAX_VALUE .
Also, I have set the Halignment property to HPos.RIGHT in columnOneConstraints so that all the
nodes in column 1 are right aligned.
Create a Scene
Let’s now create a scene with the registration form grid pane as the root node. Modify the start()
method of the main class so that it looks like this -
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Registration Form JavaFX Application");
primaryStage.show();
}
If you run the application now, you’ll get an empty window of width 800 and height 500. In the next
section, We’ll add UI controls to the application.
The gridPane.add() method takes the columnIndex , rowIndex , colSpan and rowSpan arguments.
You can add UI controls to any cell in the grid by using these arguments. columnIndex and rowIndex
starts from zero.
The headerLabel is placed on the rst cell (columnIndex : 0, rowIndex : 0) and spans two columns. We
have also added horizontal alignment and some margin to the headerLabel.
The Name, Email and Password elds are placed in subsequent rows. All the labels are placed in the
rst column and the input elds are placed in the second column.
submitButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(nameField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(),
"Form Error!", "Please enter your name");
return;
}
if(emailField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(),
"Form Error!", "Please enter your email id");
return;
}
if(passwordField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(),
"Form Error!", "Please enter a password");
return;
}
showAlert(Alert.AlertType.CONFIRMATION, gridPane.getScene().getWindow(),
"Registration Successful!", "Welcome " + nameField.getText());
}
});
In the above handler code, we show an error alert if any of the form elds are missing, otherwise, we
show a con rmation alert to the user.
We use JavaFX’s built-in Alert class to show the alert boxes to the user. Following is the
implementation of showAlert() method. -
private void showAlert(Alert.AlertType alertType, Window owner, String title, String messa
Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.initOwner(owner);
alert.show();
}
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.Window;
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Registration Form JavaFX Application");
primaryStage.show();
}
// Position the pane at the center of the screen, both vertically and horizontally
gridPane.setAlignment(Pos.CENTER);
gridPane.getColumnConstraints().addAll(columnOneConstraints, columnTwoConstrains)
return gridPane;
}
submitButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(nameField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "For
return;
}
if(emailField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "For
return;
}
if(passwordField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "For
return;
}
showAlert(Alert.AlertType.CONFIRMATION, gridPane.getScene().getWindow(),
}
});
}
Conclusion
In this blog post, we learned about JavaFX layout design and created a nice looking interactive
registration form using GridPane layout. The code for the Registration Form application can be found
on my github repository.
In the next blog post, I’ll show you how to build the same application using JavaFX FXML.
Thank you for reading. Please ask your doubts in the comment section below.
CalliCoder
Home About Contact Sitemap
Copyright © 2021 CalliCoder Privacy Policy
URL Encoder URL Decoder Base64 Encoder Base64 Decoder JSON Formatter ASCII Table