Question: n this exercise, you ll update the JavaFX MPG application to handle invalid data. When you re done, the application should display a dialog box

n this exercise, youll update the JavaFX MPG application to handle invalid data. When youre done, the application should display a dialog box that looks like this when invalid data is entered:
Test the application with invalid data 1. Open the project named ch12_ex2_MPG in the ex_starts directory. 2. Run the application and enter invalid data for miles or gallons. Then, click the Calculate button.
3. Review the resulting error messages that are displayed in the console. Then, close the application by clicking the X in the upper right corner.
Check user entries and display error messages in a dialog box 4. Add a validation class like the one shown in this chapter to the project. To keep things simple, add this class to the same package as the MpgApp class.
5. Modify the code for the event handler method so it uses the validation class to check user entries.
6. Add code that displays any messages about invalid entries in a dialog box. 7. Test the data validation by running the project and entering invalid data. When youre done testing, close the application.Exercise 12-2 Add data validation
In this exercise, you'll update the JavaFX MPG application to handle invalid
data. When you're done, the application should display a dialog box that looks
like this when invalid data is entered:
Test the application with invalid data
Open the project named ch12_ex2_MPG in the ex_starts directory.
Run the application and enter invalid data for miles or gallons. Then, click the
Calculate button.
Review the resulting error messages that are displayed in the console. Then,
close the application by clicking the "x" in the upper right corner.
Check user entries and display error messages in a dialog box
Add a validation class like the one shown in this chapter to the project. To
keep things simple, add this class to the same package as the MpgApp class.
Modify the code for the event handler method so it uses the validation class to
check user entries.
Add code that displays any messages about invalid entries in a dialog box.
Test the data validation by running the project and entering invalid data. When
you're done testing, close the application.
My code:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class MPGApplication extends Application {
@Override
public void start(Stage primaryStage){
Button btn = new Button();
TextField miles = new TextField();
TextField gallons = new TextField();
TextField mpg = new TextField();
mpg.setEditable(false);
btn.setText("Calculate");
btn.setOnAction(new EventHandler(){
@Override
public void handle(ActionEvent event){
try{
double mil = Double.parseDouble(miles.getText());
double gall = Double.parseDouble(gallons.getText());
double mpgVal = mil/gall;
mpg.setText(mpgVal+"");
}catch(Exception e){
System.out.println("Error");
}
}
});
GridPane root = new GridPane();
root.setVgap(10);
root.setHgap(10);
root.setPadding(new Insets(25,25,25,25));
root.add(new Label("Miles : "),0,0);
root.add(miles,1,0);
root.add(new Label("Gallons : "),0,1);
root.add(gallons,1,1);
root.add(new Label("MPG : "),0,2);
root.add(mpg,1,2);
root.add(btn,1,3);
Scene scene = new Scene(root,300,250);
primaryStage.setTitle("Miles Per Gallon Calculator");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}
 n this exercise, youll update the JavaFX MPG application to handle

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!