Skip to content

Commit a1e3e67

Browse files
author
Rajeev Kumar Singh
committed
Initial Commit
0 parents  commit a1e3e67

File tree

11 files changed

+342
-0
lines changed

11 files changed

+342
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
.idea
3+
out
4+
*.iml

HelloWorld/Readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## JavaFX Hello World Example with Lifecycle Methods
2+
3+
The Project contains a Simple JavaFX Hello World example with implementation of all the lifecycle methods.
4+
Compile and run `HelloWorldApplication.java` for running the program.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import javafx.application.Application;
2+
import javafx.geometry.Pos;
3+
import javafx.scene.Scene;
4+
import javafx.scene.control.Label;
5+
import javafx.stage.Stage;
6+
7+
public class HelloWorldApplication extends Application {
8+
9+
@Override
10+
public void init() throws Exception {
11+
super.init();
12+
System.out.println("Inside init() method! Perform necessary initializations here.");
13+
}
14+
15+
@Override
16+
public void start(Stage primaryStage) throws Exception {
17+
Label label = new Label("Hello World");
18+
label.setAlignment(Pos.CENTER);
19+
Scene scene = new Scene(label, 500, 350);
20+
21+
primaryStage.setTitle("Hello World Application");
22+
primaryStage.setScene(scene);
23+
primaryStage.show();
24+
}
25+
26+
@Override
27+
public void stop() throws Exception {
28+
super.stop();
29+
System.out.println("Inside stop() method! Destroy resources. Perform Cleanup.");
30+
}
31+
32+
public static void main(String[] args) {
33+
launch(args);
34+
}
35+
}

Readme.md

Whitespace-only changes.

RegistrationForm/Readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## JavaFX Registration Form Example
2+
3+
The Project explains how to create a Registration form in JavaFX.
4+
Compile and run `RegistrationFormApplication.java` for running the program.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import javafx.application.Application;
2+
import javafx.event.ActionEvent;
3+
import javafx.event.EventHandler;
4+
import javafx.geometry.HPos;
5+
import javafx.geometry.Insets;
6+
import javafx.geometry.Pos;
7+
import javafx.scene.Scene;
8+
import javafx.scene.control.*;
9+
import javafx.scene.layout.*;
10+
import javafx.scene.text.Font;
11+
import javafx.scene.text.FontWeight;
12+
import javafx.stage.Stage;
13+
import javafx.stage.Window;
14+
15+
public class RegistrationFormApplication extends Application {
16+
17+
@Override
18+
public void start(Stage primaryStage) throws Exception {
19+
primaryStage.setTitle("Registration Form JavaFX Application");
20+
21+
// Create the registration form grid pane
22+
GridPane gridPane = createRegistrationFormPane();
23+
// Add UI controls to the registration form grid pane
24+
addUIControls(gridPane);
25+
// Create a scene with registration form grid pane as the root node
26+
Scene scene = new Scene(gridPane, 800, 500);
27+
// Set the scene in primary stage
28+
primaryStage.setScene(scene);
29+
30+
primaryStage.show();
31+
}
32+
33+
34+
private GridPane createRegistrationFormPane() {
35+
// Instantiate a new Grid Pane
36+
GridPane gridPane = new GridPane();
37+
38+
// Position the pane at the center of the screen, both vertically and horizontally
39+
gridPane.setAlignment(Pos.CENTER);
40+
41+
// Set a padding of 20px on each side
42+
gridPane.setPadding(new Insets(40, 40, 40, 40));
43+
44+
// Set the horizontal gap between columns
45+
gridPane.setHgap(10);
46+
47+
// Set the vertical gap between rows
48+
gridPane.setVgap(10);
49+
50+
// Add Column Constraints
51+
52+
// columnOneConstraints will be applied to all the nodes placed in column one.
53+
ColumnConstraints columnOneConstraints = new ColumnConstraints(100, 100, Double.MAX_VALUE);
54+
columnOneConstraints.setHalignment(HPos.RIGHT);
55+
56+
// columnTwoConstraints will be applied to all the nodes placed in column two.
57+
ColumnConstraints columnTwoConstrains = new ColumnConstraints(200,200, Double.MAX_VALUE);
58+
columnTwoConstrains.setHgrow(Priority.ALWAYS);
59+
60+
gridPane.getColumnConstraints().addAll(columnOneConstraints, columnTwoConstrains);
61+
62+
return gridPane;
63+
}
64+
65+
private void addUIControls(GridPane gridPane) {
66+
// Add Header
67+
Label headerLabel = new Label("Registration Form");
68+
headerLabel.setFont(Font.font("Arial", FontWeight.BOLD, 24));
69+
gridPane.add(headerLabel, 0,0,2,1);
70+
GridPane.setHalignment(headerLabel, HPos.CENTER);
71+
GridPane.setMargin(headerLabel, new Insets(20, 0,20,0));
72+
73+
// Add Name Label
74+
Label nameLabel = new Label("Full Name : ");
75+
gridPane.add(nameLabel, 0,1);
76+
77+
// Add Name Text Field
78+
TextField nameField = new TextField();
79+
nameField.setPrefHeight(40);
80+
gridPane.add(nameField, 1,1);
81+
82+
83+
// Add Email Label
84+
Label emailLabel = new Label("Email ID : ");
85+
gridPane.add(emailLabel, 0, 2);
86+
87+
// Add Email Text Field
88+
TextField emailField = new TextField();
89+
emailField.setPrefHeight(40);
90+
gridPane.add(emailField, 1, 2);
91+
92+
// Add Password Label
93+
Label passwordLabel = new Label("Password : ");
94+
gridPane.add(passwordLabel, 0, 3);
95+
96+
// Add Password Field
97+
PasswordField passwordField = new PasswordField();
98+
passwordField.setPrefHeight(40);
99+
gridPane.add(passwordField, 1, 3);
100+
101+
// Add Submit Button
102+
Button submitButton = new Button("Submit");
103+
submitButton.setPrefHeight(40);
104+
submitButton.setDefaultButton(true);
105+
submitButton.setPrefWidth(100);
106+
gridPane.add(submitButton, 0, 4, 2, 1);
107+
GridPane.setHalignment(submitButton, HPos.CENTER);
108+
GridPane.setMargin(submitButton, new Insets(20, 0,20,0));
109+
110+
submitButton.setOnAction(new EventHandler<ActionEvent>() {
111+
@Override
112+
public void handle(ActionEvent event) {
113+
if(nameField.getText().isEmpty()) {
114+
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Form Error!", "Please enter your name");
115+
return;
116+
}
117+
if(emailField.getText().isEmpty()) {
118+
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Form Error!", "Please enter your email id");
119+
return;
120+
}
121+
if(passwordField.getText().isEmpty()) {
122+
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Form Error!", "Please enter a password");
123+
return;
124+
}
125+
126+
showAlert(Alert.AlertType.CONFIRMATION, gridPane.getScene().getWindow(), "Registration Successful!", "Welcome " + nameField.getText());
127+
}
128+
});
129+
}
130+
131+
private void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
132+
Alert alert = new Alert(alertType);
133+
alert.setTitle(title);
134+
alert.setHeaderText(null);
135+
alert.setContentText(message);
136+
alert.initOwner(owner);
137+
alert.show();
138+
}
139+
140+
public static void main(String[] args) {
141+
launch(args);
142+
}
143+
}

RegistrationFormFXML/Readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## JavaFX Registration Form example using FXML
2+
3+
The Project explains how to create a Registration form in JavaFX using FXML.
4+
Clone the Project, Import it into your favorite IDE and run `RegistrationFormApplication.java` for running the program.
5+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package javafx.example;
2+
3+
import javafx.scene.control.Alert;
4+
import javafx.stage.Window;
5+
6+
/**
7+
* Created by rajeevkumarsingh on 02/05/17.
8+
*/
9+
public class AlertHelper {
10+
11+
public static void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
12+
Alert alert = new Alert(alertType);
13+
alert.setTitle(title);
14+
alert.setHeaderText(null);
15+
alert.setContentText(message);
16+
alert.initOwner(owner);
17+
alert.show();
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package javafx.example;
2+
3+
import javafx.application.Application;
4+
import javafx.fxml.FXMLLoader;
5+
import javafx.scene.Parent;
6+
import javafx.scene.Scene;
7+
import javafx.stage.Stage;
8+
9+
public class RegistrationFormApplication extends Application {
10+
11+
@Override
12+
public void start(Stage primaryStage) throws Exception{
13+
Parent root = FXMLLoader.load(getClass().getResource("registration_form.fxml"));
14+
primaryStage.setTitle("Registration Form Application");
15+
primaryStage.setScene(new Scene(root, 800, 500));
16+
primaryStage.show();
17+
}
18+
19+
20+
public static void main(String[] args) {
21+
launch(args);
22+
}
23+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package javafx.example;
2+
3+
import javafx.event.ActionEvent;
4+
import javafx.fxml.FXML;
5+
import javafx.scene.control.Alert;
6+
import javafx.scene.control.Button;
7+
import javafx.scene.control.PasswordField;
8+
import javafx.scene.control.TextField;
9+
import javafx.stage.Window;
10+
11+
public class RegistrationFormController {
12+
@FXML
13+
private TextField nameField;
14+
15+
@FXML
16+
private TextField emailField;
17+
18+
@FXML
19+
private PasswordField passwordField;
20+
21+
@FXML
22+
private Button submitButton;
23+
24+
@FXML
25+
public void handleSubmitButtonAction(ActionEvent event) {
26+
Window owner = submitButton.getScene().getWindow();
27+
if(nameField.getText().isEmpty()) {
28+
AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Form Error!", "Please enter your name");
29+
return;
30+
}
31+
if(emailField.getText().isEmpty()) {
32+
AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Form Error!", "Please enter your email id");
33+
return;
34+
}
35+
if(passwordField.getText().isEmpty()) {
36+
AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Form Error!", "Please enter a password");
37+
return;
38+
}
39+
40+
AlertHelper.showAlert(Alert.AlertType.CONFIRMATION, owner, "Registration Successful!", "Welcome " + nameField.getText());
41+
}
42+
}

0 commit comments

Comments
 (0)