Question

In: Computer Science

Can someone show me how to make this javaFx code work? The submit button should remain...

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);
    }
}

Solutions

Expert Solution

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*/



Related Solutions

I kept getting errors trying to make this code to work, can someone show me what...
I kept getting errors trying to make this code to work, can someone show me what i am doing wrong? import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.*; import java.util.*; import java.nio.file.*; import java.nio.charset.*; import java.util.*; //@WebServlet(name = "Assignment3_1", urlPatterns = { "/ReadFile" }) public class ReadFile extends HttpServlet{ static Charset myCharset = Charset.forName("US-ASCII"); @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ response.setContentType("text/html"); PrintWriter printer = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>"); out.println("Assignment 5.1"); out.println("</title>"); out.println("<h1>Is Bitcoin money?</h1>"); out.println("</head>"); out.println("<body>");...
Can someone please show me how to do the work on this please. Question The transactions...
Can someone please show me how to do the work on this please. Question The transactions of the Fury Delivery Service are recorded in the general journal below. Instructions: 1. Post the journal entries to the attached general ledger “T” accounts. 2. Prepare a trial balance on the form provided after the “T” accounts. General Journal Date Account Titles and Explanation Debit Credit 2017 Sept. 1 Cash Common Stock (Stockholders invested cash in business) 25,000 25,000 4 Equipment Cash Notes...
Can someone show me the R code to accomplish this? In R, Construct scatter plots of...
Can someone show me the R code to accomplish this? In R, Construct scatter plots of y versus x, y versus ln(x), ln(y) versus ln(x) and 1/y versus 1/x. Include your R code in a separate file. The article “Reduction in Soluble Protein and Chlorophyll Contents in a Few Plants as Indicators of Automobile Exhaust Pollution” (Intl. J. of Environ. Studies, 1983: 239-244) reported the accompanying data on x distance from a highway (meters) and y lead content of soil...
can someone explain to me circular, double, linked list? can someone show a program on how...
can someone explain to me circular, double, linked list? can someone show a program on how you would go about using recursive with proper functions and one not using recursive but somethibg simikar! bibary search tree, preorder, post order? good explanation of linked list sorted.
Could someone please tell me what corrections I should make to this code. (Python) Here are...
Could someone please tell me what corrections I should make to this code. (Python) Here are the instructions. Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating I can't get the find function to work and I have no idea how to even go about it. For example, when type in 'find' and...
can someone make me a shopping cart for me ? i need to make a shopping...
can someone make me a shopping cart for me ? i need to make a shopping cart ,but i have no idea about how to do this i have made 3 line of items , this is one of the pruduct line line 1 ------------------------------------- <!DOCTYPE html> <html lang="en"> <head> <style> .div1 { border: 2px outset red; background-color: lightblue; text-align: center; } </style> </head> <!-- body --> <body style="background-color:silver; "class="main-layout position_head"> <!-- loader --> </li> </ul> </section> </nav> </section> </header>...
Can someone show me how to do a test for lack of fit for the following...
Can someone show me how to do a test for lack of fit for the following data? Please show all work for an up vote. Thanks. y x4 x7 x9 29.5 1.5 4 0 27.9 1.175 3 0 25.9 1.232 3 0 29.9 1.121 3 0 29.9 0.988 3 0 30.9 1.24 3 1 28.9 1.501 3 0 35.9 1.225 3 0 31.5 1.552 3 0 31 0.975 2 0 30.9 1.121 3 0 30 1.02 2 1 36.9 1.664...
Can someone show me the steps of how to solve this? A program needs to access...
Can someone show me the steps of how to solve this? A program needs to access the following pages: 1, 2, 3, 4, 2, 1, 3, 2, 1, 4, 2, 3 There are 3 initially empty frames, how many page faults will there be respectively, if we use First-in-First-out, and Farthest-in-Future page replacement algorithms? A) 7 and 7 B) 7 and 6 --  Correct Answer   C) 6 and 6 D) 6 and 5
Using C# visual basics Can someone show a calculator with a button that does a FOR...
Using C# visual basics Can someone show a calculator with a button that does a FOR LOOP about 10x time and does a print out of a name 10x
This is an Android Question. I am trying to make a FAB(Floating Action Button) show me...
This is an Android Question. I am trying to make a FAB(Floating Action Button) show me a different page when I press it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT