In: Computer Science
Develop a JavaFX GUI application called Registration that implements a user interface for registering for a web site. The application should have labeled text fields for the Full Name, User Name, Password, Student Id, and a TextAreafor About Me. Include a button labeled Send. When the Send button is clicked, your program should print the contents of all fields (with labels) to standard output using println()statements.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//Registration.java
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Registration extends Application {
@Override
public void start(Stage primaryStage) {
//creating a GridPane to contain UI components
GridPane root = new GridPane();
//setting padding
root.setPadding(new Insets(10));
//and spacing betweeen components
root.setHgap(10);
root.setVgap(10);
//initializing each label and text field
Label nameLbl = new Label("Full Name: ");
TextField nameTxt = new TextField();
Label usernameLbl = new Label("Username: ");
TextField usernameTxt = new TextField();
Label passLbl = new Label("Password: ");
//using a PasswordField for password, so the contents are hidden when typed.
TextField passTxt = new PasswordField();
Label idLbl = new Label("Student ID: ");
TextField idTxt = new TextField();
Label aboutLbl = new Label("About Me: ");
TextArea aboutTxt = new TextArea();
Button sendBtn = new Button("Send");
//adding each component to the grid pane in proper column and row
root.add(nameLbl, 0, 0); //column 0, row 0
root.add(nameTxt, 1, 0);//column 1, row 0
root.add(usernameLbl, 0, 1);
root.add(usernameTxt, 1, 1);
root.add(passLbl, 0, 2);
root.add(passTxt, 1, 2);
root.add(idLbl, 0, 3);
root.add(idTxt, 1, 3);
root.add(aboutLbl, 0, 4);
//adding text area to column 0, row 5 occupying 2 columns width
root.add(aboutTxt, 0, 5, 2, 1);
root.add(sendBtn, 0, 6);
//adding event listener for button
sendBtn.setOnAction(e -> {
//fetching contents
String name = nameTxt.getText();
String username = usernameTxt.getText();
String pass = passTxt.getText();
String id = idTxt.getText();
String abtme = aboutTxt.getText();
//printing to console.
System.out.println("Name: " + name);
System.out.println("Username: " + username);
System.out.println("Password: " + pass);
System.out.println("ID: " + id);
System.out.println("About Me:\n" + abtme);
});
//setting up and displaying a scene
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Registration");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
/*OUTPUT*/