Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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);
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

=+e) Are there eight points in a row on the same side of the mean?

Answered: 1 week ago