Question
*****JAVAFX***** I have created a Form in JavaFX. This is a start. I now need help in adding fields for each course that the student
*****JAVAFX*****
I have created a Form in JavaFX. This is a start. I now need help in adding fields for each course that the student is enrolled in (such as: SDEV200, SDEV264, CPIN239, CPIN269), but maybe the student only has 2 courses (SDEV264, SDEV200). I would like for them to be able to enter any number of classes necessary. Then the student can enter the course, and each course entered will have a field so that they can enter the number of hours they spent studying in this course. When finished I need a button that will give a dialog box for the total of number of hours they spent on each course with a grand total of hours spent studying.
package application;
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.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.Window;
public class StudentForm extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Time Tracker");
//create student form pane
GridPane gridPane = createStudentFormPane();
//add UI controls
addUIControls(gridPane);
//create a scene with the student form gripPane as the root node
Scene scene = new Scene(gridPane, 500, 600);
//set scene in primary stage
primaryStage.setScene(scene);
primaryStage.show();
}
private GridPane createStudentFormPane() {
//new Grid Pane
GridPane gridPane = new GridPane();
//Position the pane
gridPane.setAlignment(Pos.CENTER);
//set Padding
gridPane.setPadding(new Insets(20, 20, 20, 20));
//set horizontal gap between columns
gridPane.setHgap(10);
//set vertical gap between rows
gridPane.setVgap(10);
//Column One Constraints
ColumnConstraints columnOneConstraints = new ColumnConstraints(100, 100, Double.MAX_VALUE);
columnOneConstraints.setHalignment(HPos.RIGHT);
//Column Two Constraints
ColumnConstraints columnTwoConstrains = new ColumnConstraints(100, 100, Double.MAX_VALUE);
columnTwoConstrains.setHgrow(Priority.ALWAYS);
gridPane.getColumnConstraints().addAll(columnOneConstraints, columnTwoConstrains);
return gridPane;
}
private void addUIControls(GridPane gridPane) {
//Header
Label headerLabel = new Label("Time Tracking Form");
headerLabel.setFont(Font.font("Arial", FontWeight.BOLD, 24));
gridPane.add(headerLabel, 0,0,2,1);
GridPane.setHalignment(headerLabel, HPos.CENTER);
GridPane.setMargin(headerLabel, new Insets(20,0,20,0));
//Add first name label
Label firstNameLabel = new Label("First Name: ");
gridPane.add(firstNameLabel, 0, 1);
//add first name text field
TextField firstNameField = new TextField();
firstNameField.setPrefHeight(20);
gridPane.add(firstNameField, 1, 1);
//Add last name label
Label lastNameLabel = new Label("Last Name: ");
gridPane.add(lastNameLabel, 0, 2);
//add last name text field
TextField lastNameField = new TextField();
lastNameField.setPrefHeight(20);
gridPane.add(lastNameField, 1, 2);
//Add address label
Label addressLabel = new Label("Address: ");
gridPane.add(addressLabel, 0, 3);
//add address text field
TextField addressField = new TextField();
addressField.setPrefHeight(20);
gridPane.add(addressField, 1, 3);
//Add city label
Label cityLabel = new Label("City: ");
gridPane.add(cityLabel, 0, 4);
//add city text field
TextField cityField = new TextField();
cityField.setPrefHeight(20);
gridPane.add(cityField, 1, 4);
//Add state label
Label stateLabel = new Label("State: ");
gridPane.add(stateLabel, 0, 5);
//add state text field
TextField stateField = new TextField();
stateField.setPrefHeight(20);
gridPane.add(stateField, 1, 5);
//Add zip label
Label zipLabel = new Label("Zip Code: ");
gridPane.add(zipLabel, 0, 6);
//add zip text field
TextField zipField = new TextField();
zipField.setPrefHeight(20);
gridPane.add(zipField, 1, 6);
//Add email label
Label emailLabel = new Label("Email: ");
gridPane.add(emailLabel, 0, 7);
//add email text field
TextField emailField = new TextField();
emailField.setPrefHeight(20);
gridPane.add(emailField, 1, 7);
//Add phone label
Label phoneLabel = new Label("Phone: ");
gridPane.add(phoneLabel, 0, 8);
//add phone text field
TextField phoneField = new TextField();
phoneField.setPrefHeight(20);
gridPane.add(phoneField, 1, 8);
//Add degree label
Label degreeLabel = new Label("Degree: ");
gridPane.add(degreeLabel, 0, 9);
//add degree text field
TextField degreeField = new TextField();
degreeField.setPrefHeight(20);
gridPane.add(degreeField, 1, 9);
// Add Submit Button
Button submitButton = new Button("Submit");
submitButton.setPrefHeight(40);
submitButton.setDefaultButton(true);
submitButton.setPrefWidth(100);
gridPane.add(submitButton, 0, 10, 2, 1);
GridPane.setHalignment(submitButton, HPos.CENTER);
GridPane.setMargin(submitButton, new Insets(20, 0,20,0));
submitButton.setOnAction(new EventHandler
@Override
public void handle(ActionEvent event) {
if(firstNameField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(),
"Form Error!", "Please enter your first name");
return;
}
if(lastNameField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(),
"Form Error!", "Please enter your last name");
return;
}
if(addressField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(),
"Form Error!", "Please enter an address");
return;
}
if(cityField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(),
"Form Error!", "Please enter an city");
return;
}
if(stateField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(),
"Form Error!", "Please enter an state");
return;
}
if(zipField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(),
"Form Error!", "Please enter an zip");
}
if(emailField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(),
"Form Error!", "Please enter an email");
}
if(phoneField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(),
"Form Error!", "Please enter a phone");
}
if(degreeField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(),
"Form Error!", "Please enter a degree");
}
showAlert(Alert.AlertType.CONFIRMATION, gridPane.getScene().getWindow(),
"Form Successful!", "Thank you! " + firstNameField.getText());
}
});
}
private void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.initOwner(owner);
alert.show();
}
public static void main(String[] args) {
launch(args);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started