Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator."— Presentation transcript:

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator Application 13.2 Event Handlers 13.3 Event Handler Registration 13.4 Handling a ChangeEvent 13.5 Wrap-Up Tutorial 13 – Enhancing the Interest Calculator Application Introduction to Event Handling

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objective In this tutorial, you will learn to: –Create an event handler. –Understand how objects are used to represent events. –Handle the ChangeEvent which is generated when the value in a JSpinner is changed.

3 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 13.1 Test-Driving the Enhanced Interest Calculator Application

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 13.1 Test-Driving the Enhanced Interest Calculator Application (Cont.) Figure 13.1 Running Interest Calculator application.

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 13.1 Test-Driving the Enhanced Interest Calculator Application (Cont.) Enter values in the components Figure 13.2 Entering values in the running Interest Calculator application.

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 13.1 Test-Driving the Enhanced Interest Calculator Application (Cont.) JTextArea is cleared Figure 13.3 Clearing the output.

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 13.1 Test-Driving the Enhanced Interest Calculator Application (Cont.) Figure 13.4 Calculating the new account balance. Output is cleared

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 13.1 Test-Driving the Enhanced Interest Calculator Application (Cont.) Figure 13.5 Changing the value in a JSpinner. Output is clearedNumber of years is changed

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 13.1 Test-Driving the Enhanced Interest Calculator Application (Cont.) Figure 13.6 Clearing the Interest Calculator application. JTextField s are cleared and JSpinner is set to 1 JTextArea is clearedClear the components and reset the year to 1

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 13.2 Event Handlers When the user clicks the Calculate JButton Get the values for the principal, interest rate and years Display a header in the JTextArea to label the output For each year, starting at 1 and ending with the number of years entered, Calculate and display the year Calculate and display the current value of the investment When the user changes the value in the Years: JSpinner Clear the JTextArea When the user types in the Principal: JTextField Clear the JTextArea When the user types in the Interest rate: JTextField Clear the JTextArea When the user clicks the Clear JButton Clear the JTextArea Clear the JTextFields Reset the JSpinner to the default value

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 13.2 Event Handlers (Cont.)

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 13.2 Event Handlers (Cont.) Event driven application –Waits for certain actions (or events) –These actions generate event objects in response –Event handlers (e.g. actionPerformed methods) are then called by the event objects

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 13.2 Event Handlers (Cont.) Declaration of event handler actionPerformed Figure 13.8 Examining calculateJButton ’s actionPerformed event handler. Event objects –ActionEvent generated when a user clicks a JButton –KeyEvent and MouseEvent are other types of event objects Access modifier –keyword public allows objects of other classes to access this event handler

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 13.2 Event Handlers (Cont.) Figure 13.9 Finishing calculateJButton ’s actionPerformed event handler. Call programmer- declared method

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 13.2 Event Handlers (Cont.) Figure 13.10 Running the updated Interest Calculator application.

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 13.3 Event Handler Registration Event handler registration –Each event handler must be registered with its corresponding component

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 13.3 Event Handler Registration (Cont.) Figure 13.11 Completing the clearJButton event handler. Call programmer- declared method

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 13.3 Event Handler Registration (Cont.) Figure 13.12 Event handler registration for the clearJButton. Register event handler for clearJButton Method addActionListener –registers the actionPerformed event handler

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 13.3 Event Handler Registration (Cont.) Figure 13.13 Event handler registration for the calculateJButton. Register event handler for calculateJButton

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 13.3 Event Handler Registration (Cont.) Figure 13.14 Declaring method clearJButtonActionPerformed. Declare method to clear the values of the components and reset the JSpinner to 1

21 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 13.3 Event Handler Registration (Cont.) Figure 13.15 Clearing the values in the components. Clear components and reset the JSpinner to 1 The setValue method –Sets the value property of the JSpinner to an Integer object representing the value 1

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 13.3 Event Handler Registration (Cont.) Figure 13.16 Running the updated Interest Calculator application. Output is cleared

23 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 13.4 Handling a ChangeEvent Figure 13.17 Event handler stateChanged for a ChangeEvent. Event handler stateChanged Event object ChangeEvent –Generated by changing the value in a JSpinner Event handler stateChanged –Called as a result of a new ChangeEvent

24 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 13.4 Handling a ChangeEvent (Cont.) Figure 13.18 Calling the programmer-declared method yearJSpinnerStateChanged from event handler stateChanged. Call programmer- declared method from event handler

25 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 13.4 Handling a ChangeEvent (Cont.) Figure 13.19 Registering the event handler stateChanged to handle ChangeEvent s. Call method addChangeListener

26 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 13.4 Handling a ChangeEvent (Cont.) Figure 13.20 Clearing the accountJTextArea. Clear the JTextArea

27 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 13.4 Handling a ChangeEvent (Cont.) Figure 13.21 Completed Interest Calculator application responding to ChangeEvent by clearing JTextArea.

28  2004 Prentice Hall, Inc. All rights reserved. Outline 28 Interest- Calculator.java (1 of 11) 1 // Tutorial 13: InterestCalculator.java 2 // Calculate the total value of an investment. 3 import java.text.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 import javax.swing.*; 7 import javax.swing.event.*; 8 9 public class InterestCalculator extends JFrame 10 { 11 // JLabel and JTextField for principal 12 private JLabel principalJLabel; 13 private JTextField principalJTextField; 14 15 // JLabel and JTextField for interest rate 16 private JLabel rateJLabel; 17 private JTextField rateJTextField; 18 19 // JLabel and JSpinner for years 20 private JLabel yearsJLabel; 21 private JSpinner yearsJSpinner; 22

29  2004 Prentice Hall, Inc. All rights reserved. Outline 29 Interest- Calculator.java (2 of 11) 23 // JLabel, JTextArea and JScrollPane for yearly balances 24 private JLabel accountJLabel; 25 private JTextArea accountJTextArea; 26 private JScrollPane accountJScrollPane; 27 28 // JButton to calculate the yearly balances 29 private JButton calculateJButton; 30 31 // JButton to clear the components 32 private JButton clearJButton; 33 34 // no-argument constructor 35 public InterestCalculator() 36 { 37 createUserInterface(); 38 } 39 40 // create and position GUI components; register event handlers 41 private void createUserInterface() 42 { 43 // get content pane and set layout to null 44 Container contentPane = getContentPane(); 45 contentPane.setLayout( null ); 46

30  2004 Prentice Hall, Inc. All rights reserved. Outline 30 Interest- Calculator.java (3 of 11) 47 // set up principalJLabel 48 principalJLabel = new JLabel(); 49 principalJLabel.setText( "Principal:" ); 50 principalJLabel.setBounds( 16, 16, 56, 24 ); 51 contentPane.add( principalJLabel ); 52 53 // set up principalJTextField 54 principalJTextField = new JTextField(); 55 principalJTextField.setHorizontalAlignment( JTextField.RIGHT ); 56 principalJTextField.setBounds( 96, 16, 104, 24 ); 57 contentPane.add( principalJTextField ); 58 principalJTextField.addKeyListener( 59 60 new KeyAdapter() // anonymous inner class 61 { 62 // event handler called when principalJTextField 63 // is edited 64 public void keyPressed( KeyEvent event ) 65 { 66 principalJTextFieldKeyPressed( event ); 67 } 68 69 } // end anonymous inner class 70 71 ); // end call to addKeyListener

31  2004 Prentice Hall, Inc. All rights reserved. Outline 31 Interest- Calculator.java (4 of 11) 72 73 // set up rateJLabel 74 rateJLabel = new JLabel(); 75 rateJLabel.setText( "Interest rate:" ); 76 rateJLabel.setBounds( 16, 56, 80, 24 ); 77 contentPane.add( rateJLabel ); 78 79 // set up rateJTextField 80 rateJTextField = new JTextField(); 81 rateJTextField.setHorizontalAlignment( JTextField.RIGHT ); 82 rateJTextField.setBounds( 96, 56, 104, 24 ); 83 contentPane.add( rateJTextField ); 84 rateJTextField.addKeyListener( 85 86 new KeyAdapter() // anonymous inner class 87 { 88 // event handler called when rateJTextField is edited 89 public void keyPressed( KeyEvent event ) 90 { 91 rateJTextFieldKeyPressed( event ); 92 } 93 94 } // end anonymous inner class 95 96 ); // end call to addKeyListener

32  2004 Prentice Hall, Inc. All rights reserved. Outline 32 Interest- Calculator.java (5 of 11) 97 98 // set up yearsJLabel 99 yearsJLabel = new JLabel(); 100 yearsJLabel.setText( "Years:" ); 101 yearsJLabel.setBounds( 16, 96, 48, 24 ); 102 contentPane.add( yearsJLabel ); 103 104 // set up yearsJSpinner 105 yearsJSpinner = new JSpinner( 106 new SpinnerNumberModel( 1, 1, 10, 1 ) ); 107 yearsJSpinner.setBounds( 96, 96, 104, 24 ); 108 contentPane.add( yearsJSpinner ); 109 yearsJSpinner.addChangeListener( 110 111 new ChangeListener() // anonymous inner class 112 { 113 // event handler called when value in 114 // yearsJSpinner changes 115 public void stateChanged( ChangeEvent event ) 116 { 117 yearsJSpinnerStateChanged( event ); 118 } 119 120 } // end anonymous inner class 121 Calling programmer- declared method from event handler Calling method addChangeListener Event handler stateChanged

33  2004 Prentice Hall, Inc. All rights reserved. Outline 33 Interest- Calculator.java (6 of 11) 122 ); // end call to addChangeListener 123 124 // set up calculateJButton 125 calculateJButton = new JButton(); 126 calculateJButton.setText( "Calculate" ); 127 calculateJButton.setBounds( 216, 16, 100, 24 ); 128 contentPane.add( calculateJButton ); 129 calculateJButton.addActionListener( 130 131 new ActionListener() // anonymous inner class 132 { 133 // event handler called when calculate button is clicked 134 public void actionPerformed( ActionEvent event ) 135 { 136 calculateJButtonActionPerformed( event ); 137 } 138 139 } // end anonymous inner class 140 141 ); // end call to addActionListener 142 Call programmer- declared method from event handler Registering event handler for calculateJButton Event handler actionPerformed

34  2004 Prentice Hall, Inc. All rights reserved. Outline 34 Interest- Calculator.java (7 of 11) 143 // set up clearJButton 144 clearJButton = new JButton(); 145 clearJButton.setBounds( 216, 56, 100, 24 ); 146 clearJButton.setText( "Clear" ); 147 contentPane.add( clearJButton ); 148 clearJButton.addActionListener( 149 150 new ActionListener() // anonymous inner class 151 { 152 // event handler called when clearJButton is clicked 153 public void actionPerformed( ActionEvent event ) 154 { 155 clearJButtonActionPerformed( event ); 156 } 157 158 } // end anonymous inner class 159 160 ); // end call to addActionListener 161 162 // set up accountJLabel 163 accountJLabel = new JLabel(); 164 accountJLabel.setText( "Yearly account balance:" ); 165 accountJLabel.setBounds( 16, 136, 150, 24 ); 166 contentPane.add( accountJLabel ); 167 Call programmer- declared method from event handler Registering event handler for clearJButton Event handler actionPerformed

35  2004 Prentice Hall, Inc. All rights reserved. Outline 35 Interest- Calculator.java (8 of 11) 168 // set up accountJTextArea 169 accountJTextArea = new JTextArea(); 170 accountJTextArea.setEditable( false ); 171 172 // set up accountJScrollPane 173 accountJScrollPane = new JScrollPane( accountJTextArea ); 174 accountJScrollPane.setBounds( 16, 160, 300, 88 ); 175 contentPane.add( accountJScrollPane ); 176 177 // set properties of application’s window 178 setTitle( "Interest Calculator" ); // set title bar text 179 setSize( 340, 300 ); // set window size 180 setVisible( true ); // show window 181 182 } // end method createUserInterface 183 184 // clear the accountJTextArea 185 private void rateJTextFieldKeyPressed( KeyEvent event ) 186 { 187 accountJTextArea.setText( "" ); 188 189 } // end method rateJTextFieldKeyPressed 190

36  2004 Prentice Hall, Inc. All rights reserved. Outline 36 Interest- Calculator.java (9 of 11) 191 // clear the accountJTextArea 192 private void principalJTextFieldKeyPressed( KeyEvent event ) 193 { 194 accountJTextArea.setText( "" ); 195 196 } // end method principalJTextFieldKeyPressed 197 198 // calculate yearly values of investment 199 private void calculateJButtonActionPerformed( ActionEvent event ) 200 { 201 // declare variables to store user input 202 double principal = Double.parseDouble( 203 principalJTextField.getText() ); 204 double rate = Double.parseDouble( rateJTextField.getText() ); 205 Integer integerObject = ( Integer ) yearsJSpinner.getValue(); 206 int year = integerObject.intValue(); 207 208 accountJTextArea.setText( "Year\tAmount on Deposit" ); 209 DecimalFormat dollars = new DecimalFormat( "$0.00" ); 210

37  2004 Prentice Hall, Inc. All rights reserved. Outline 37 Interest- Calculator.java (10 of 11) 211 // calculate the total value for each year 212 for ( int count = 1; count <= year; count++ ) 213 { 214 double amount = principal * 215 Math.pow( ( 1 + rate / 100 ), count ); 216 accountJTextArea.append( "\n" + count + "\t" + 217 dollars.format( amount ) ); 218 219 } // end for 220 221 } // end method calculateJButtonActionPerformed 222 223 // clear GUI components 224 private void clearJButtonActionPerformed( ActionEvent event ) 225 { 226 // clear the JTextFields 227 principalJTextField.setText( "" ); 228 rateJTextField.setText( "" ); 229 230 // clear the JTextArea 231 accountJTextArea.setText( "" ); 232 233 // reset the value of the JSpinner 234 yearsJSpinner.setValue( new Integer( 1 ) ); 235 Programmer- declared method Clear JTextField s Reset JSpinner to original value Clear JTextArea

38  2004 Prentice Hall, Inc. All rights reserved. Outline 38 Interest- Calculator.java (11 of 11) 236 } // end method clearJButtonActionPerformed 237 238 // clear the JTextArea 239 private void yearsJSpinnerStateChanged( ChangeEvent event ) 240 { 241 accountJTextArea.setText( "" ); 242 243 } // end method yearsJSpinnerStateChanged 244 245 // main method 246 public static void main( String args[] ) 247 { 248 InterestCalculator application = new InterestCalculator(); 249 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 250 251 } // end method main 252 253 } // end class InterestCalculator Programmer- declared method Clear JTextArea


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator."

Similar presentations


Ads by Google