Question: need help adding event handler to my javafx gui program, starting code is at the bottom. follow the steps below using event handler for the

need help adding event handler to my javafx gui program, starting code is at the bottom.

follow the steps below using event handler for the calculator button in javafx.

Add an event handler to the Calculate button that reads values from the total, tip percentage, and number of people text fields and computes the amount that each person should pay, assuming that the check is divided evenly. This amount should be properly formatted (rounded to two decimal places and preceded by a dollar sign) and replace the text in the label. If any of the text fields are empty or contain something other than numeric values when the Calculate button is pressed, then pressing it should have no effect (i.e. the exception stack trace should be shown on the console for debugging use by the programmer, but the GUI should not crash or end its execution).

***starting code***

package lab4a;

import javafx.application.Application; import static javafx.application.Application.launch; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage;

/** * * @author * / * */ public class lab4a extends Application {

@Override public void start(Stage primaryStage) {

VBox box = new VBox(); Label label = new Label(); label.setText("Total: "); box.getChildren().add(label); TextField tf = new TextField(); box.getChildren().add(tf); Label label1 = new Label("Tip Percantage: "); box.getChildren().add(label1); TextField tf1 = new TextField(); box.getChildren().add(tf1); Label label2 = new Label("Number of People: "); box.getChildren().add(label2); TextField tf2 = new TextField(); box.getChildren().add(tf2); Label label3 = new Label("Each person pays:"); box.getChildren().add(label3); Label label4 = new Label(""); box.getChildren().add(label4);

HBox box1 = new HBox(); Button btn1 = new Button(); btn1.setText("Calculate"); box1.getChildren().add(btn1);

BorderPane root = new BorderPane();

root.setCenter(box); root.setBottom(box1);

Scene scene = new Scene(root, 250, 250);

primaryStage.setTitle("Check Divider"); primaryStage.setScene(scene); primaryStage.show(); }

/** * @param args the command line arguments */ public static void main(String[] args) { launch(args); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!