Question: 13.6 (Tip Calculator Modification) The Tip Calculator app from Section 12.5 does not need a Button to perform its calculations. Reimplement this app to use
13.6 (Tip Calculator Modification) The Tip Calculator app from Section 12.5 does not need a Button to perform its calculations. Reimplement this app to use property listeners to perform the calculations whenever the user modifies the bill amount or changes the custom tip percentage. Also use a property binding to update the Label that displays the tip percentage.
// Fig. 12.19: TipCalculator.java // Main application class that loads and displays the Tip Calculator's GUI. import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage;
public class TipCalculator extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("TipCalculator.fxml"));
Scene scene = new Scene(root); // attach scene graph to scene stage.setTitle("Tip Calculator"); // displayed in window's title bar stage.setScene(scene); // attach scene to stage stage.show(); // display the stage }
public static void main(String[] args) { // create a TipCalculator object and call its start method launch(args); } }
=========================================================================
// TipCalculatorController.java // Controller that handles calculateButton and tipPercentageSlider events import java.math.BigDecimal; import java.math.RoundingMode; import java.text.NumberFormat; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Label; import javafx.scene.control.Slider; import javafx.scene.control.TextField;
public class TipCalculatorController { // formatters for currency and percentages private static final NumberFormat currency = NumberFormat.getCurrencyInstance(); private static final NumberFormat percent = NumberFormat.getPercentInstance(); private BigDecimal tipPercentage = new BigDecimal(0.15); // 15% default // GUI controls defined in FXML and used by the controller's code @FXML private TextField amountTextField;
@FXML private Label tipPercentageLabel;
@FXML private Slider tipPercentageSlider;
@FXML private TextField tipTextField;
@FXML private TextField totalTextField;
// calculates and displays the tip and total amounts @FXML private void calculateButtonPressed(ActionEvent event) { try { BigDecimal amount = new BigDecimal(amountTextField.getText()); BigDecimal tip = amount.multiply(tipPercentage); BigDecimal total = amount.add(tip);
tipTextField.setText(currency.format(tip)); totalTextField.setText(currency.format(total)); } catch (NumberFormatException ex) { amountTextField.setText("Enter amount"); amountTextField.selectAll(); amountTextField.requestFocus(); } }
// called by FXMLLoader to initialize the controller public void initialize() { // 0-4 rounds down, 5-9 rounds up currency.setRoundingMode(RoundingMode.HALF_UP); // listener for changes to tipPercentageSlider's value tipPercentageSlider.valueProperty().addListener( new ChangeListener() { @Override public void changed(ObservableValue ov, Number oldValue, Number newValue) { tipPercentage = BigDecimal.valueOf(newValue.intValue() / 100.0); tipPercentageLabel.setText(percent.format(tipPercentage)); } } ); } }
=========================================================================
// TipCalculator.fxml
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
