In: Computer Science
Can someone show me how to make this javaFx code work? The submit button should remain disabled until: ● There is text in all three fields. ● The two password fields have the same value. When Submit is clicked, display an Alert that says “Account Created!” When Quit is clicked, display an Alert that asks the user if they are sure they want to quit. If they click OK, quit the program with System.exit(0). If they click Cancel, the program keeps running. import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.Event; import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.stage.Stage; import javafx.scene.layout.GridPane; import javafx.scene.control.Alert.AlertType; import java.util.Optional; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { Button SubmitHandler = new Button("Submit"); Button Quit = new Button("Quit"); Label title = new Label("Create an Account"); Label label1 = new Label("User Name"); Label label2 = new Label("Password"); Label label3 = new Label("Re-enter Password"); TextField userName = new TextField(); PasswordField passWord = new PasswordField(); PasswordField rePassWord = new PasswordField(); HBox hBox1 = new HBox(title); HBox hBox2 = new HBox(51, label1, userName); HBox hBox3 = new HBox(60, label2, passWord); HBox hBox4 = new HBox(10, label3, rePassWord); HBox hBox5 = new HBox(55, SubmitHandler, Quit); hBox1.setAlignment(Pos.CENTER); hBox2.setAlignment(Pos.BASELINE_RIGHT); hBox3.setAlignment(Pos.BASELINE_RIGHT); hBox4.setAlignment(Pos.CENTER); hBox5.setAlignment(Pos.BASELINE_LEFT); hBox1.setPadding(new Insets(10, 10, 10, 10)); hBox2.setPadding(new Insets(10, 10, 10, 10)); hBox3.setPadding(new Insets(10, 10, 10, 10)); hBox4.setPadding(new Insets(10, 10, 10, 10)); hBox5.setPadding(new Insets(10, 10, 10, 10)); GridPane gridPane = new GridPane(); gridPane.add(hBox1, 0, 0); gridPane.add(hBox2, 0, 1); gridPane.add(hBox3, 0, 2); gridPane.add(hBox4, 0, 3); gridPane.add(hBox5, 0, 4); Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); primaryStage.setTitle("In class 6"); primaryStage.setScene(new Scene(gridPane)); primaryStage.show(); } private EventHandler<ActionEvent> QuitHandler = event -> { Alert quitAlert = new Alert(AlertType.CONFIRMATION); quitAlert.setTitle(""); quitAlert.getButtonTypes().add(ButtonType.NO); quitAlert.setHeaderText("Save Before Quitting?"); Optional<ButtonType> result = quitAlert.showAndWait(); if (result.isPresent() && result.get() == ButtonType.OK) System.exit(0); if (result.isPresent() && result.get() == ButtonType.NO) System.exit(0); }; public static void main(String[] args) { launch(args); } }
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. Thanks
// Main.java
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.layout.GridPane;
import javafx.scene.control.Alert.AlertType;
import java.util.Optional;
public class Main extends Application {
//moving the declaration of submit button and input fields to outside the
//start method, because we need to access them from outside
Button submitButton;
TextField userName;
PasswordField passWord, rePassWord;
@Override
public void start(Stage primaryStage) throws Exception {
//initializing submit button
submitButton = new Button("Submit");
//disabling it initially
submitButton.setDisable(true);
//adding SubmitHandler as action listener
submitButton.setOnAction(SubmitHandler);
Button Quit = new Button("Quit");
Quit.setOnAction(QuitHandler);
Label title = new Label("Create an Account");
Label label1 = new Label("User Name");
Label label2 = new Label("Password");
Label label3 = new Label("Re-enter Password");
userName = new TextField();
//adding text change property to each text field, so that it will call
//the update method upon text change
userName.textProperty().addListener(e -> update());
passWord = new PasswordField();
passWord.textProperty().addListener(e -> update());
rePassWord = new PasswordField();
rePassWord.textProperty().addListener(e -> update());
HBox hBox1 = new HBox(title);
HBox hBox2 = new HBox(51, label1, userName);
HBox hBox3 = new HBox(60, label2, passWord);
HBox hBox4 = new HBox(10, label3, rePassWord);
HBox hBox5 = new HBox(55, submitButton, Quit);
hBox1.setAlignment(Pos.CENTER);
hBox2.setAlignment(Pos.BASELINE_RIGHT);
hBox3.setAlignment(Pos.BASELINE_RIGHT);
hBox4.setAlignment(Pos.CENTER);
hBox5.setAlignment(Pos.BASELINE_LEFT);
hBox1.setPadding(new Insets(10, 10, 10, 10));
hBox2.setPadding(new Insets(10, 10, 10, 10));
hBox3.setPadding(new Insets(10, 10, 10, 10));
hBox4.setPadding(new Insets(10, 10, 10, 10));
hBox5.setPadding(new Insets(10, 10, 10, 10));
GridPane gridPane = new GridPane();
gridPane.add(hBox1, 0, 0);
gridPane.add(hBox2, 0, 1);
gridPane.add(hBox3, 0, 2);
gridPane.add(hBox4, 0, 3);
gridPane.add(hBox5, 0, 4);
//Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("In class 6");
primaryStage.setScene(new Scene(gridPane));
primaryStage.show();
}
//handler for quit action
private EventHandler<ActionEvent> QuitHandler = event -> {
Alert quitAlert = new Alert(AlertType.CONFIRMATION);
quitAlert.setHeaderText("Are you sure you want to quit?");
Optional<ButtonType> result = quitAlert.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK) {
System.exit(0);
}
};
//handler for submit action
private EventHandler<ActionEvent> SubmitHandler = event -> {
//creating and displaying a success alert dialog saying "Account Created!"
Alert successAlert = new Alert(AlertType.INFORMATION);
successAlert.setContentText("Account Created!");
successAlert.setHeaderText("Success!");
successAlert.showAndWait();
};
//this method gets called whenever any text field gets updated
public void update() {
//getting texts from all three fields
String user = userName.getText();
String pass = passWord.getText();
String confirmPass = rePassWord.getText();
//ensuring that all three fields are non empty and password and confirm
//password fields are same
if (!user.equals("") && !pass.equals("") && !confirmPass.equals("") && pass.equals(confirmPass)) {
//enabling the button
submitButton.setDisable(false);
} else {
//disabling the button
submitButton.setDisable(true);
}
}
public static void main(String[] args) {
launch(args);
}
}
/*OUTPUT*/