© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application 7.2 Constructing the Dental Payment Application 7.3Using JCheckBoxes 7.4Using a Dialog to Display a Message 7.5Logical Operators 7.6Wrap-Up Tutorial 7 – Dental Payment Application Introducing JCheckBox es and Message Dialogs
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Use JCheckBox es to allow users to select options. –Use dialogs to display messages. –Use logical operators to form more complex conditions.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Dental Payment Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Dental Payment Application (Cont.) Figure 7.1 Dental Payment application without input entered. JCheckBox components (unchecked)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 Figure 7.2 Message dialog appears when no name is entered and/or no JCheckBox es are selected. 7.1 Test-Driving the Dental Payment Application (Cont.) Message dialog Run the Dental Payment application – cd c:\Examples\Tutorial07\ CompletedApplication\DentalPayment – java DentalPayment Click OK JButton to close the dialog
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Dental Payment Application (Cont.) Figure 7.3 Dental Payment application with input entered and total price of services displayed. JCheckBox components (checked)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Dental Payment Application When the user clicks the Calculate JButton: Input patient name If user has not entered a patient name or has not selected any JCheckBoxes Display error message in dialog Else If Cleaning JCheckBox is selected Add cost of a cleaning to the total If Cavity Filling JCheckBox is selected Add cost of a cavity filling to the total If X-Ray JCheckBox is selected Add cost of an x-ray to the total Display total price of services rendered in dollar format
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Dental Payment Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using JCheckBox es JCheckBox es – small gray square either blank or containing a small checkmark – Properties: – selected property: whether or not a JCheckBox is selected – text property: the text just beside a JCheckBox – bounds property: location of JCheckBox – Methods: – isSelected – returns true if JCheckBox is selected – setText – sets text of JCheckBox – setBounds – sets bounds of JCheckBox state button: a button that has either an on or off state
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using JCheckBox es (Cont.) Figure 7.5 Changing the text and bounds properties of cleaningJCheckBox. Use methods setBounds and setText to change JCheckBox properties
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using JCheckBox es (Cont.) Figure 7.6 Changing text and bounds properties of cavityFillingJCheckBox.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using JCheckBox es (Cont.) Figure 7.7 Changing text and bounds properties of xRayJCheckBox.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using JCheckBox es (Cont.) Figure 7.8 Application running after customizing three JCheckBox es.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using JCheckBox es (Cont.) Use method isSelected to determine if a JCheckBox is selected.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using JCheckBox es (Cont.) Figure 7.9 Determining if a JCheckBox has been selected. Calling method isSelected Add this code to add the proper amount to total
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using JCheckBox es (Cont.) Figure 7.10 Application running without input. Application calculates a bill of $0.00 when no JCheckBox es are checked
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using JCheckBox es (Cont.) Figure 7.11 Application running with services selected, but no patient name entered.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using a Dialog to Display a Message Figure 7.12 Message dialog displayed by the application. Close buttonDialog sized to accommodate contents Title barIcon indicates the tone of the message OK JButton allows user to close the dialog
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using a Dialog to Display a Message (Cont.) Figure 7.13 Adding an if statement to event handler calculateJButtonActionPerformed. Add this code to verify patient name was entered
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using a Dialog to Display a Message (Cont.) Figure 7.14 Message dialog code that displays a message to users. Add an error message Use method showMessageDialog to display error message in separate window
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using a Dialog to Display a Message (Cont.) Figure 7.15 Adding an else part to an if statement. Add an else statement Add the right brace to end else statement
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using a Dialog to Display a Message (Cont.) Figure 7.17 Application running without a name entered.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using a Dialog to Display a Message (Cont.) Figure 7.18 Application running with a name entered, but without any JCheckBox es selected. User must now enter a nameApplication calculates a bill of $0.00 when a name is entered and no JCheckBox es are checked
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using a Dialog to Display a Message (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Logical Operators Logical operators –&& (conditional AND) –|| (conditional OR) –^ (boolean logical exclusive OR) –! (logical negation)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Logical Operators (Cont.) Using Conditional AND ( && ) if ( gender.equals( "Female" ) && age >= 65 ) { seniorFemales += 1; }
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Logical Operators (Cont.) Using Conditional OR ( || ) if ( semesterAverage >= 90 || finalExam >= 90 ) { JOptionPane.showMessageDialog( null, "Student grade is A.", "Student Grade", JOptionPane.INFORMATION_MESSAGE ); }
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Logical Operators (Cont.) Using Boolean Logical Exclusive OR ( ^ )
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Logical Operators (Cont.) Using Logical Negation ( ! ) if ( !( grade == value ) ) { displayLabel.setText( "They are not equal!" ); }
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Logical Operators (Cont.) Figure 7.24 Using the && and || logical operators. Inserting a complex expression
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Logical Operators (Cont.) Figure 7.25 Message dialog displayed if no name is entered and no JCheckBox es are selected.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Logical Operators (Cont.) Figure 7.26 Message dialog displayed for incorrect input.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Logical Operators (Cont.) Figure 7.27 Running the completed application with correct input.
2004 Prentice Hall, Inc. All rights reserved. Outline 34 DentalPayment.java (1 of 10) 1 // Tutorial 7: DentalPayment.java 2 // This program calculates the total cost of the bill for a 3 // patient at a dental office. 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.text.*; 7 import javax.swing.*; 8 9 public class DentalPayment extends JFrame 10 { 11 // JLabel that displays header on application window 12 private JLabel dentalPaymentFormJLabel; // JLabel and JTextField for patient name 15 private JLabel patientNameJLabel; 16 private JTextField patientNameJTextField; // JCheckBox and JLabel for cleaning 19 private JCheckBox cleaningJCheckBox; 20 private JLabel cleaningPriceJLabel; // JCheckBox and JLabel for cavity filling 23 private JCheckBox cavityFillingJCheckBox; 24 private JLabel cavityFillingPriceJLabel; 25
2004 Prentice Hall, Inc. All rights reserved. Outline 35 DentalPayment.java (2 of 10) 26 // JCheckBox and JLabel for X-Ray 27 private JCheckBox xRayJCheckBox; 28 private JLabel xRayPriceJLabel; // JLabel and JTextField for total fee 31 private JLabel totalJLabel; 32 private JTextField totalJTextField; // JButton to initiate calculation of fee 35 private JButton calculateJButton; // no-argument constructor 38 public DentalPayment() 39 { 40 createUserInterface(); 41 } // create and position GUI components; register event handlers 44 private void createUserInterface() 45 { 46 // get content pane for attaching GUI components 47 Container contentPane = getContentPane(); // enable explicit positioning of GUI components 50 contentPane.setLayout( null );
2004 Prentice Hall, Inc. All rights reserved. Outline 36 DentalPayment.java (3 of 10) // set up dentalPaymentFormJLabel 53 dentalPaymentFormJLabel = new JLabel(); 54 dentalPaymentFormJLabel.setBounds( 19, 19, 235, 28 ); 55 dentalPaymentFormJLabel.setText( "Dental Payment Form" ); 56 dentalPaymentFormJLabel.setFont( 57 new Font( "Default", Font.PLAIN, 22 ) ); 58 dentalPaymentFormJLabel.setHorizontalAlignment( 59 JLabel.CENTER ); 60 contentPane.add( dentalPaymentFormJLabel ); // set up patientNameJLabel 63 patientNameJLabel = new JLabel(); 64 patientNameJLabel.setBounds( 19, 65, 91, 21 ); 65 patientNameJLabel.setText( "Patient name:" ); 66 contentPane.add( patientNameJLabel ); // set up patientNameJTextField 69 patientNameJTextField = new JTextField(); 70 patientNameJTextField.setBounds( 132, 65, 117, 21 ); 71 contentPane.add( patientNameJTextField ); 72
2004 Prentice Hall, Inc. All rights reserved. Outline 37 DentalPayment.java (4 of 10) 73 // set up cleaningJCheckBox 74 cleaningJCheckBox = new JCheckBox(); 75 cleaningJCheckBox.setBounds( 16, 112, 122, 24 ); 76 cleaningJCheckBox.setText( "Cleaning" ); 77 contentPane.add( cleaningJCheckBox ); // set up cleaningPriceJLabel 80 cleaningPriceJLabel = new JLabel(); 81 cleaningPriceJLabel.setBounds( 211, 112, 38, 24 ); 82 cleaningPriceJLabel.setText( "$35" ); 83 cleaningPriceJLabel.setHorizontalAlignment( JLabel.RIGHT ); 84 contentPane.add( cleaningPriceJLabel ); // set up cavityFillingJCheckBox 87 cavityFillingJCheckBox = new JCheckBox(); 88 cavityFillingJCheckBox.setBounds( 16, 159, 122, 24 ); 89 cavityFillingJCheckBox.setText( "Cavity Filling" ); 90 contentPane.add( cavityFillingJCheckBox ); 91 Customizing cleaningJCheckBox Customizing cavityFilling JCheckBox
2004 Prentice Hall, Inc. All rights reserved. Outline 38 DentalPayment.java (5 of 10) 92 // set up cavityFillingPriceJLabel 93 cavityFillingPriceJLabel = new JLabel(); 94 cavityFillingPriceJLabel.setBounds( 211, 159, 38, 24 ); 95 cavityFillingPriceJLabel.setText( "$150" ); 96 cavityFillingPriceJLabel.setHorizontalAlignment( 97 JLabel.RIGHT ); 98 contentPane.add( cavityFillingPriceJLabel ); // set up xRayJCheckBox 101 xRayJCheckBox = new JCheckBox(); 102 xRayJCheckBox.setBounds( 16, 206, 122, 24 ); 103 xRayJCheckBox.setText( "X-Ray" ); 104 contentPane.add( xRayJCheckBox ); // set up xRayPriceJLabel 107 xRayPriceJLabel = new JLabel(); 108 xRayPriceJLabel.setBounds( 211, 206, 38, 24 ); 109 xRayPriceJLabel.setText( "$85" ); 110 xRayPriceJLabel.setHorizontalAlignment( JLabel.RIGHT ); 111 contentPane.add( xRayPriceJLabel ); 112 Customizing x RayJCheckBox
2004 Prentice Hall, Inc. All rights reserved. Outline 39 DentalPayment.java (6 of 10) 113 // set up totalJLabel 114 totalJLabel = new JLabel(); 115 totalJLabel.setBounds( 144, 256, 41, 21 ); 116 totalJLabel.setText( "Total:" ); 117 contentPane.add( totalJLabel ); // set up totalJTextField 120 totalJTextField = new JTextField(); 121 totalJTextField.setBounds( 192, 256, 56, 21 ); 122 totalJTextField.setEditable( false ); 123 totalJTextField.setHorizontalAlignment( JTextField.CENTER ); 124 contentPane.add( totalJTextField ); // set up calculateJButton 127 calculateJButton = new JButton(); 128 calculateJButton.setBounds( 155, 296, 94, 24 ); 129 calculateJButton.setText( "Calculate" ); 130 contentPane.add( calculateJButton ); 131 calculateJButton.addActionListener( new ActionListener() // anonymous inner class 134 {
2004 Prentice Hall, Inc. All rights reserved. Outline 40 DentalPayment.java (7 of 10) 135 // event handler called when user clicks calculateJButton 136 public void actionPerformed( ActionEvent event ) 137 { 138 calculateJButtonActionPerformed( event ); 139 } } // end anonymous inner class ); // end call to addActionListener // set properties of application’s window 146 setTitle( "Dental Payment" ); // set title bar string 147 setSize( 272, 364 ); // set window size 148 setVisible( true ); // display window } // end method createUserInterface // calculate cost of patient's visit 153 private void calculateJButtonActionPerformed( ActionEvent event ) 154 { 155 // get patient's name 156 String patient = patientNameJTextField.getText(); 157 Obtaining patient name
2004 Prentice Hall, Inc. All rights reserved. Outline 41 DentalPayment.java (8 of 10) 158 // display error message if no name entered or no box selected 159 if ( ( patient.equals( "" ) ) || 160 ( !cleaningJCheckBox.isSelected() && 161 !cavityFillingJCheckBox.isSelected() && 162 !xRayJCheckBox.isSelected() ) ) 163 { 164 // display error message 165 JOptionPane.showMessageDialog( null, 166 "Please enter a name and check at least one item.", 167 "Missing information", JOptionPane.WARNING_MESSAGE ); 168 } 169 else // otherwise, do calculations 170 { 171 double total = 0.0; // sum of all services provided // if patient had a cleaning 174 if ( cleaningJCheckBox.isSelected() ) 175 { 176 total += 35; // add 35 to total 177 } 178 Using logical operators and JCheckBox method isSelected Displaying a JOptionPane message dialog Examining the selected property of cleaningJCheckBox
2004 Prentice Hall, Inc. All rights reserved. Outline 42 DentalPayment.java (9 of 10) 179 // if patient had cavity filled 180 if ( cavityFillingJCheckBox.isSelected() ) 181 { 182 total += 150; // add 150 to total 183 } // if patient had x-ray taken 186 if ( xRayJCheckBox.isSelected() ) 187 { 188 total += 85; // add 85 to total 189 } // specify display format 192 DecimalFormat dollars = new DecimalFormat( "$0.00" ); // display total 195 totalJTextField.setText( dollars.format( total ) ); } // end else } // end method calculateJButtonActionPerformed 200 Examining the selected property of cavityFillingJCh eckBox Examining the selected property of xRayJCheckBox Format and display total
2004 Prentice Hall, Inc. All rights reserved. Outline 43 DentalPayment.java (10 of 10) 201 // main method 202 public static void main( String [] args ) 203 { 204 DentalPayment application = new DentalPayment(); 205 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class DentalPayment