Question
HELP! :( I am working on a JAVAFX program that will output a working GUI. This prorgam is a form for the user to fill
HELP! :(
I am working on a JAVAFX program that will output a working GUI. This prorgam is a form for the user to fill out, it has buttons that will cause pop ups, new scenes, and exit. The buttons are supposed to do a few different tasks. To better explain it, these are the instructions from my teacher:
Your GUI should have the following required fields:
o First Name
o Last Name
o Primary Email Address
o Secondary Email Address ?
-The required fields labels will turn red if an entry is missed and a pop-up Alert will notify the user when the Enter button is pressed ?
- As the red-labeled fields are corrected, they should turn to black again once Enter is clicked ?
- Populate the ComboBox control for the State with two-letter abbreviations (See code snippets below) ?
- Only one CheckBox control can be selected at a time ?
-Use Regex expressions to verify Phone Numbers, Email and Zip Codes (See code snippets below) ?
- The Clear button clears all entries and sets the ComboBox to the first state in the list (AL) ?
- The Enter button pops-up another pane that: o Displays all entries
o Prints N/A for Phone numbers not included (only one can be selected)
o Includes an Edit and Confirm button ?
- The Edit button simply closes the Verify pane ?
- The Confirm button:
o Pops-up a thank you dialog box with a custom icon (See code snippets below)
o Closes the verify pane and thank you dialog box when the OK button is clicked
o Clears the entries on the main pane ?
- The Exit button closes the program ?
- Use entry verification
I completed a bunch of it but still having trouble with the layout/placement of code to get it to do what I actually want. Also, there are a few things I don't know how to do such as:
- clear all entries when the confirm button is clicked
- Turn entries red when they are missed (i have the pop up alert code in there but I don't think it works) when the ENTER button is clicked.
- When the red labels are fixed, they should turn back to black when ENTER is clicked.
- I don't know hoe to implement the regex verifiations:
? Phone Numbers will be in the form nnn-nnn-nnnn: o "\\d{3}[-\\.\\s]\\d{3}[-\\.\\s]\\d{4}" ?
Zip codes can be in the form nnnnn or nnnnn-nnnn: o "^[0-9]{5}(?:-[0-9]{4})?$" ?
Emails should be in the form text@text.com or text.text@text.com as such: o "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"
- I need the clear button on the main form page to clear all entries.& set combo box to first state "AL"
- I need the enter button to pop up a new pane that will:
o Displays all entries
o Prints N/A for Phone numbers not included (only one can be selected)
o Includes an Edit and Confirm button
- The Confirm button:
o Pops-up a thank you dialog box with a custom icon
o Closes the verify pane and thank you dialog box when the OK button is clicked
o Clears the entries on the main pane
THIS IS MY CODE SO FAR:
package egentile_guilayoutwk2;
//Imports import javafx.application.Application; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.GridPane; import javafx.geometry.Pos; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight;
//Begin Class Egentile_GUILayoutWK2 public class Egentile_GUILayoutWK2 extends Application {
public static void main(String[] args) { launch(args); } TextField txtfName, txtlName, txtMI, txtStreeAdd, txtCity, txtZip, txtPhone, txtEmail, txtSecEmail; @Override public void start(Stage primaryStage) throws Exception { //Create BorderPane BorderPane bPane = new BorderPane(); //Create GridPane GridPane gPane = new GridPane(); //border and grid pane attributes gPane.setAlignment(Pos.CENTER); gPane.setPadding(new Insets(5, 5, 5, 5)); bPane.setPadding(new Insets(5, 5, 5, 5)); gPane.setHgap(5); gPane.setVgap(5);
//Create drop down box for city option final ComboBox stateComboBox = new ComboBox(); stateComboBox.getItems().addAll( "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA","WA", "WV", "WI", "WY" ); //Define label nodes Label lblfName = new Label("First Name:"); Label lbllName = new Label("Last Name:"); Label lblmInt = new Label("Mi:"); Label lblsAdd = new Label("Street Address:"); Label lblcITY = new Label("City: "); Label lblzIP = new Label("Zip: "); Label lblPhoneNum = new Label("Phone Number: "); Label lbleMAIL = new Label("Primary email: "); Label lblsecEmail = new Label("Secondary email: "); //Create centered gridpane GridPane form = new GridPane(); form.setAlignment(Pos.CENTER); form.setPadding(new Insets(11.5, 12.5, 13.5, 14.5)); form.setHgap(5.5); form.setVgap(5.5); //Create Checkboxes with labels CheckBox homeCheckBox = new CheckBox("Home"); CheckBox workCheckBox = new CheckBox("Work"); CheckBox cellCheckBox = new CheckBox("Cell"); //Set Buttons Button BtnEnter = new Button("Enter"); Button BtnClr = new Button("Clear"); Button BtnExit = new Button("Exit"); //Add label nodes to pane and set constraints gPane.add(lblfName, 0, 0); GridPane.setConstraints(lblfName, 0, 0, 1, 1); gPane.add(lbllName, 2, 0); GridPane.setConstraints(lbllName, 2, 0, 1, 1); gPane.add(lblmInt, 5, 0); GridPane.setConstraints(lblmInt, 5, 0, 1, 1); gPane.add(lblsAdd, 0, 1); GridPane.setConstraints(lblsAdd, 0, 1, 1, 1); gPane.add(lblcITY, 2, 1); GridPane.setConstraints(lblcITY, 2, 1, 1, 1); gPane.add(lblzIP, 5, 1); GridPane.setConstraints(lblzIP, 5, 1, 1, 1); gPane.add(lblPhoneNum, 0, 2); GridPane.setConstraints(lblPhoneNum, 0, 2, 1, 1); gPane.add(lbleMAIL, 0, 3); GridPane.setConstraints(lbleMAIL, 0, 3, 1, 1); gPane.add(lblsecEmail, 0, 4, 1, 1); GridPane.setConstraints(lblsecEmail, 0, 4); //Define TextField nodes txtfName = new TextField(); txtlName = new TextField(); txtMI = new TextField(); txtStreeAdd = new TextField(); txtCity = new TextField(); txtZip = new TextField(); txtPhone = new TextField(); txtEmail = new TextField(); txtSecEmail = new TextField(); //Set text field column widths txtfName.setPrefColumnCount(10); txtlName.setPrefColumnCount(10); txtMI.setPrefColumnCount(6); txtStreeAdd.setPrefColumnCount(15); txtCity.setPrefColumnCount(2); txtZip.setPrefColumnCount(5); txtPhone.setPrefColumnCount(10); txtEmail.setPrefColumnCount(20); txtSecEmail.setPrefColumnCount(10); //Add text field nodes to pane and set constraints gPane.add(txtfName, 1, 0); GridPane.setConstraints(txtfName, 1, 0, 1, 1); gPane.add(txtlName, 3, 0); GridPane.setConstraints(txtlName, 3, 0, 2, 1); gPane.add(txtMI, 6, 0); GridPane.setConstraints(txtMI, 6, 0, 1, 1); gPane.add(txtStreeAdd, 1, 1); GridPane.setConstraints(txtStreeAdd, 1, 1, 1, 1); gPane.add(stateComboBox, 3, 1); GridPane.setConstraints(txtCity, 3, 1, 1, 1); gPane.add(txtZip, 6, 1); GridPane.setConstraints(txtZip, 6, 1, 1, 1); gPane.add(txtPhone, 1, 2); GridPane.setConstraints(txtPhone, 1, 2, 1, 1); gPane.add(homeCheckBox, 2, 2, 6, 1); gPane.add(workCheckBox, 3, 2, 4, 1); gPane.add(cellCheckBox, 4, 2, 6, 1); gPane.add(txtEmail, 1, 3); GridPane.setConstraints(txtEmail, 1, 3, 1, 1); gPane.add(txtSecEmail, 1, 4); GridPane.setConstraints(txtSecEmail, 1, 4, 1, 1); gPane.setAlignment(Pos.CENTER); gPane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5)); gPane.setHgap(5.5); gPane.setVgap(5.5); gPane.setStyle("-fx-border-color: wheat"); bPane.setCenter(gPane); bPane.setTop(getVBoxTop()); bPane.setStyle("-fx-background-color: ghostwhite"); bPane.setBottom(getPaneBottom());
BtnEnter.setOnAction((ActionEvent e) -> { //NOT SURE HOW TO SET LABELS TO RED if (txtfName.getText().isEmpty() | txtlName.getText().isEmpty() | txtMI.getText().isEmpty() | txtStreeAdd.getText().isEmpty() | txtCity.getText().isEmpty() | txtZip.getText().isEmpty() | txtPhone.getText().isEmpty() | txtEmail.getText().isEmpty() | txtSecEmail.getText().isEmpty()) { Alert alert = new Alert(AlertType.WARNING); alert.setTitle("Warning"); alert.setHeaderText("Required Fields Empty"); alert.setContentText("The fields highlighted in red must be filled " + "out. Please try again."); alert.showAndWait(); ConfirmBox.display("Verify Information"); } else { } });
BtnExit.setOnAction((ActionEvent e) -> { System.exit(0); }); BtnClr.setOnAction((ActionEvent e) -> { clearfields(); stateComboBox.setValue("AL"); }); homeCheckBox.setOnAction((ActionEvent e) -> { if (homeCheckBox.isSelected()) { workCheckBox.setSelected(false); cellCheckBox.setSelected(false); } }); workCheckBox.setOnAction((ActionEvent e) -> { if (workCheckBox.isSelected()) { homeCheckBox.setSelected(false); cellCheckBox.setSelected(false); } }); cellCheckBox.setOnAction((ActionEvent e) -> { if (cellCheckBox.isSelected()) { workCheckBox.setSelected(false); homeCheckBox.setSelected(false); } });
//Create scene to display gridpane and title
Scene scene = new Scene(bPane, 800, 428); primaryStage.setTitle("Custom Surfboard Co."); primaryStage.setScene(scene); primaryStage.show(); }
public void clearfields() { txtfName.clear(); txtlName.clear(); txtMI.clear(); txtStreeAdd.clear(); txtCity.clear(); txtZip.clear(); txtPhone.clear(); txtEmail.clear(); txtSecEmail.clear(); } private VBox getVBoxTop() { VBox vHeaders = new VBox(); Label lblHeader1 = new Label("Get Your Custom Board!"); Label lblHeader2 = new Label("Please fill out the information below " + "and click the Submit button"); lblHeader1.setFont(Font.font("Courier New", FontWeight.BOLD, 24)); vHeaders.getChildren().add(lblHeader1); lblHeader2.setFont(Font.font("Arial", FontPosture.ITALIC, 15)); vHeaders.getChildren().add(lblHeader2); vHeaders.setAlignment(Pos.CENTER); VBox.setMargin(lblHeader1, new Insets(10, 10, 0, 10)); lblHeader1.setStyle("-fx-text-fill: darksalmon;"); lblHeader2.setStyle("-fx-text-fill: darksalmon"); return vHeaders; } private VBox getVBoxBottom() { VBox Buttons = new VBox(); Button BtnEnter = new Button("Enter"); Button BtnClr = new Button("Clear"); Button BtnExit = new Button("Exit"); BtnEnter.setMaxWidth(50); BtnClr.setMaxWidth(50); BtnExit.setMaxWidth(50); VBox.setMargin(BtnEnter, new Insets(5, 0, 5, 0)); VBox.setMargin(BtnClr, new Insets(5, 0, 5, 0)); VBox.setMargin(BtnExit, new Insets(5, 0, 5, 0)); Buttons.getChildren().addAll(BtnEnter, BtnClr, BtnExit); Buttons.setAlignment(Pos.CENTER); Buttons.setPrefWidth(740); return Buttons; }
private HBox getHBoxBottom() { HBox hBoxB = new HBox(); hBoxB.setPadding(new Insets(10, 10, 0, 20)); ImageView imageOne = new ImageView(new Image("surfbnw.jpg")); imageOne.setFitHeight(200); imageOne.setFitWidth(200); ImageView imageTwo = new ImageView(new Image("water.jpg")); imageTwo.setFitHeight(200); imageTwo.setFitWidth(200); ImageView imageThree = new ImageView(new Image("palmbnw.jpg")); imageThree.setFitHeight(200); imageThree.setFitWidth(200); hBoxB.getChildren().addAll(imageOne, imageTwo, imageThree); hBoxB.setAlignment(Pos.CENTER); hBoxB.setPrefWidth(740); return hBoxB; } private HBox getPaneBottom(){ HBox bottomPort = new HBox(); bottomPort.getChildren().add(getVBoxBottom()); bottomPort.getChildren().add(getHBoxBottom()); return bottomPort; } }//End Class Egentile_GUILayoutWK2
I ALSO HAVE A SUBCLASS (not sure if i should be using this though): -for confirm box
package egentile_guilayoutwk2;
import static javafx.application.Application.launch; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ButtonBar.ButtonData; import javafx.scene.control.ButtonType; import javafx.scene.control.Dialog; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.stage.Modality; import javafx.stage.Stage;
//Imports //Begin Subclass ConfirmBox class ConfirmBox extends StackPane {
static boolean answer;
public static void main(String[] args) { launch(args); } public static void display(String title) { Stage window = new Stage(); window.initModality(Modality.APPLICATION_MODAL); window.setTitle(title); window.setWidth(250); Label label = new Label();
//create two buttons Button edit = new Button("EDIT"); Button confirm = new Button("CONFIRM"); edit.setOnAction(e -> { answer = true; window.close(); });
confirm.setOnAction(e -> { answer = true; Dialog dialog = new Dialog<>(); dialog.setTitle("Entry Confirmed"); dialog.setHeaderText("Thank you for joining our family!"); dialog.setContentText("Your information has been entered into " + "our database. Thank you for your interest in" + " Surf n' Sun Co.!"); dialog.setGraphic(new ImageView(new Image ("surfnsunlogo.jpg"))); ButtonType okType = new ButtonType("Okay", ButtonData.OK_DONE); dialog.getDialogPane().getButtonTypes().add(okType); dialog.showAndWait(); }); VBox layout = new VBox(10); layout.getChildren().addAll(label, edit, confirm); layout.setAlignment(Pos.CENTER); Scene scene2 = new Scene(layout); window.setScene(scene2); window.showAndWait(); }
} //End Subclass ConfirmBox
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