© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 11.1 Test-Driving the Security Panel Application 11.2 Introducing the switch Multiple-Selection Statement 11.3 Constructing the Security Panel Application 11.4Wrap-Up Tutorial 11 – Security Panel Application Introducing the switch Multiple-Selection Statement, Date and DateFormat
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Use the switch multiple-selection statement. –Use case labels. –Display a date and time. –Use a JPasswordField. –Use a Date to determine the system’s current date and time. –Use a DateFormat to format the date and time.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test Driving the Security Panel Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test Driving the Security Panel Application (Cont.) Figure 11.1 Security Panel application. Keypad Output JTextArea JPasswordField
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test Driving the Security Panel Application (Cont.) Figure 11.2 Asterisks displayed in the SecurityCode: JPasswordField. JPasswordField displays one asterisk ( * ) for each numeric key the user presses (so no one can see the actual security code entered) Enter the security code 1212 JPasswordField displays asterisks rather than the typed characters
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test Driving the Security Panel Application (Cont.) Figure 11.3 Security Panel displaying the Access Denied message. Message indicating that an invalid security code was entered Press # to submit your security code Press C to clear your security code
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test Driving the Security Panel Application (Cont.) Figure 11.4 Security Panel application confirming a valid security code entry. Message displayed when a valid security code is entered Enter 1006 to log on with a valid security code
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introducing the switch Multiple-Selection Statement if ( grade == 'A' ) { displayJLabel.setText( "Excellent!" ); } else if ( grade == 'B' ) { displayJLabel.setText( "Very good!" ); } else if ( grade == 'C' ) { displayJLabel.setText( "Good." ); } else if ( grade == 'D' ) { displayJLabel.setText( "Poor." ); } else if ( grade == 'F' ) { displayJLabel.setText( "Failure." ); } else { displayJLabel.setText( "Invalid grade." ); } Multiple selections with a nested if … else statement
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introducing the switch Multiple-Selection Statement (Cont.) switch statement: multiple selection statement –Controlling expression –case labels –default case –Only types char, byte, short, and int can be tested in a switch statement –break statement Char –one of Java’s eight primitive types –Character constant (character literal) –Represented as a character within single quotes
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introducing the switch Multiple-Selection Statement (Cont.) switch ( grade ) { case 'A': displayJLabel.setText( "Excellent!" ); break; case 'B': displayJLabel.setText( "Very good!" ); break; case 'C': displayJLabel.setText( "Good." ); break; case 'D': displayJLabel.setText( "Poor." ); break; case 'F': displayJLabel.setText( "Failure." ); break; default: displayJLabel.setText( "Invalid grade." ); }
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introducing the switch Multiple-Selection Statement (Cont.) Figure 11.5 switch multiple-selection statement UML activity diagram.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application When the user clicks a numeric JButton Get the JButton’s digit Append the digit to the text in the JPasswordField When the user clicks the # JButton Get the security code input by the user from the JPasswordField Clear the JPasswordField switch based on the security code variable case where access code is 7, 8 or 9 Store text “Restricted Access” in a String variable case where access code equals 1645 Store text “Technician” in a String variable case where access code equals 8345 Store text “Custodian” in a String variable case where access code equals 9998 or is in the range 1006 to 1008 Store text “Scientist” in a String variable default case where none of the preceding cases match Store text “Access Denied” in a String variable Display a message in the JTextArea with current time and the String variable’s contents
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.) Figure 11.7 Setting the securityCodeJPasswordField ’s bounds and editable properties. Echo characters (masked characters) Set character displayed using the setEchoChar method Set the bounds and editable properties of the JPasswordField
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.) Figure 11.8 Security Panel application after customizing the JPasswordField.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.) Figure 11.9 Storing the access code and clearing the Security code: JPasswordField. Declare message Store the access codeClear the JPasswordField
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.) Figure Adding a switch statement to the method. Beginning of the switch statement
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.) Figure Adding case labels to the switch statement. Multiple case labels result in same message
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.) Figure Finishing the switch statement. Access code 1645 for technicians Access code 8345 for custodians Access codes 9998, 1006, 1007 and 1008 for scientists
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.) Figure Adding a default case to the switch statement. switch statements can have at most one default statement default case at the end of the switch statement
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.) Figure Outputting the current date, the time and the message. The DateFormat object is similar to a DecimalFormat object –Allows you to output a date and time Format for the date and time Add the date and time to the message
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.) Figure Appending a one to the end of the security code. Add 1 to the end of the security code Use the + operator to append (concatenate) String s to other String s
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.) Figure Coding event handlers for 2 JButton and 3 JButton ; other JButton s would be similar. Add 2 to the end of the security code Add 3 to the end of the security code
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.) Figure Clearing the Security Code: JPasswordField. Clear the security code
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Security Panel Application (Cont.) Figure Completed Security Panel application.
2004 Prentice Hall, Inc. All rights reserved. Outline 26 SecurityPanel.java (1 of 19) 1 // Tutorial 11: SecurityPanel.java 2 // Enable user to enter security codes specifying access privileges. 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import java.text.DateFormat; 7 import java.util.Date; 8 9 public class SecurityPanel extends JFrame 10 { 11 // JLabel and JPasswordField for user to input security code 12 private JLabel securityCodeJLabel; 13 private JPasswordField securityCodeJPasswordField; // JButtons to represent security keypad 16 private JButton oneJButton; 17 private JButton twoJButton; 18 private JButton threeJButton; 19 private JButton fourJButton; 20 private JButton fiveJButton; 21 private JButton sixJButton; 22 private JButton sevenJButton; 23 private JButton eightJButton; 24 private JButton nineJButton; 25 private JButton clearJButton;
2004 Prentice Hall, Inc. All rights reserved. Outline 27 SecurityPanel.java (2 of 19) 26 private JButton zeroJButton; 27 private JButton enterJButton; // JLabel, JTextArea and JScrollPane to display access log 30 private JLabel accessLogJLabel; 31 private JTextArea accessLogJTextArea; 32 private JScrollPane accessLogJScrollPane; // no-argument constructor 35 public SecurityPanel() 36 { 37 createUserInterface(); 38 } // create and position GUI components; register event handlers 41 private void createUserInterface() 42 { 43 // get content pane for attaching GUI components 44 Container contentPane = getContentPane(); // enable explicit positioning of GUI components 47 contentPane.setLayout( null ); 48
2004 Prentice Hall, Inc. All rights reserved. Outline 28 SecurityPanel.java (3 of 19) 49 // set up securityCodeJLabel 50 securityCodeJLabel = new JLabel(); 51 securityCodeJLabel.setBounds( 16, 16, 90, 21 ); 52 securityCodeJLabel.setText( "Security code:" ); 53 contentPane.add( securityCodeJLabel ); // set up securityCodeJPasswordField 56 securityCodeJPasswordField = new JPasswordField(); 57 securityCodeJPasswordField.setBounds( 114, 16, 172, 26 ); 58 securityCodeJPasswordField.setEditable( false ); 59 contentPane.add( securityCodeJPasswordField ); // set up oneJButton 62 oneJButton = new JButton(); 63 oneJButton.setBounds( 80, 64, 50, 50 ); 64 oneJButton.setText( "1" ); 65 contentPane.add( oneJButton ); 66 oneJButton.addActionListener( new ActionListener() // anonymous inner class 69 { 70 // event handler called when oneJButton is pressed 71 public void actionPerformed( ActionEvent event ) 72 { 73 oneJButtonActionPerformed( event ); Set the bounds and editable properties of the JPasswordField
2004 Prentice Hall, Inc. All rights reserved. Outline 29 SecurityPanel.java (4 of 19) 74 } } // end anonymous inner class ); // end call to addActionListener // set up twoJButton 81 twoJButton = new JButton(); 82 twoJButton.setBounds( 130, 64, 50, 50 ); 83 twoJButton.setText( "2" ); 84 contentPane.add( twoJButton ); 85 twoJButton.addActionListener( new ActionListener() // anonymous inner class 88 { 89 // event handler called when twoJButton is pressed 90 public void actionPerformed( ActionEvent event ) 91 { 92 twoJButtonActionPerformed( event ); 93 } } // end anonymous inner class ); // end call to addActionListener 98
2004 Prentice Hall, Inc. All rights reserved. Outline 30 SecurityPanel.java (5 of 19) 99 // set up threeJButton 100 threeJButton = new JButton(); 101 threeJButton.setBounds( 180, 64, 50, 50 ); 102 threeJButton.setText( "3" ); 103 contentPane.add( threeJButton ); 104 threeJButton.addActionListener( new ActionListener() // anonymous inner class 107 { 108 // event handler called when threeJButton is pressed 109 public void actionPerformed( ActionEvent event ) 110 { 111 threeJButtonActionPerformed( event ); 112 } } // end anonymous inner class ); // end call to addActionListener // set up fourJButton 119 fourJButton = new JButton(); 120 fourJButton.setBounds( 80, 114, 50, 50 ); 121 fourJButton.setText( "4" ); 122 contentPane.add( fourJButton ); 123 fourJButton.addActionListener(
2004 Prentice Hall, Inc. All rights reserved. Outline 31 SecurityPanel.java (6 of 19) new ActionListener() // anonymous inner class 126 { 127 // event handler called when fourJButton is pressed 128 public void actionPerformed( ActionEvent event ) 129 { 130 fourJButtonActionPerformed( event ); 131 } } // end anonymous inner class ); // end call to addActionListener // set up fiveJButton 138 fiveJButton = new JButton(); 139 fiveJButton.setBounds( 130, 114, 50, 50 ); 140 fiveJButton.setText( "5" ); 141 contentPane.add( fiveJButton ); 142 fiveJButton.addActionListener( 143
2004 Prentice Hall, Inc. All rights reserved. Outline 32 SecurityPanel.java (7 of 19) 144 new ActionListener() // anonymous inner class 145 { 146 // event handler called when fiveJButton is pressed 147 public void actionPerformed( ActionEvent event ) 148 { 149 fiveJButtonActionPerformed( event ); 150 } } // end anonymous inner class ); // end call to addActionListener // set up sixJButton 157 sixJButton = new JButton(); 158 sixJButton.setBounds( 180, 114, 50, 50 ); 159 sixJButton.setText( "6" ); 160 contentPane.add( sixJButton ); 161 sixJButton.addActionListener( new ActionListener() // anonymous inner class 164 { 165 // event handler called when sixJButton is pressed 166 public void actionPerformed( ActionEvent event ) 167 { 168 sixJButtonActionPerformed( event ); 169 }
2004 Prentice Hall, Inc. All rights reserved. Outline 33 SecurityPanel.java (8 of 19) } // end anonymous inner class ); // end call to addActionListener // set up sevenJButton 176 sevenJButton = new JButton(); 177 sevenJButton.setBounds( 80, 164, 50, 50 ); 178 sevenJButton.setText( "7" ); 179 contentPane.add( sevenJButton ); 180 sevenJButton.addActionListener( new ActionListener() // anonymous inner class 183 { 184 // event handler called when sevenJButton is pressed 185 public void actionPerformed( ActionEvent event ) 186 { 187 sevenJButtonActionPerformed( event ); 188 } } // end anonymous inner class ); // end call to addActionListener 193
2004 Prentice Hall, Inc. All rights reserved. Outline 34 SecurityPanel.java (9 of 19) 194 // set up eightJButton 195 eightJButton = new JButton(); 196 eightJButton.setBounds( 130, 164, 50, 50 ); 197 eightJButton.setText( "8" ); 198 contentPane.add( eightJButton ); 199 eightJButton.addActionListener( new ActionListener() // anonymous inner class 202 { 203 // event handler called when eightJButton is pressed 204 public void actionPerformed( ActionEvent event ) 205 { 206 eightJButtonActionPerformed( event ); 207 } } // end anonymous inner class ); // end call to addActionListener // set up nineJButton 214 nineJButton = new JButton(); 215 nineJButton.setBounds( 180, 164, 50, 50 ); 216 nineJButton.setText( "9" ); 217 contentPane.add( nineJButton ); 218 nineJButton.addActionListener(
2004 Prentice Hall, Inc. All rights reserved. Outline 35 SecurityPanel.java (10 of 19) new ActionListener() // anonymous inner class 221 { 222 // event handler called when nineJButton is pressed 223 public void actionPerformed( ActionEvent event ) 224 { 225 nineJButtonActionPerformed( event ); 226 } } // end anonymous inner class ); // end call to addActionListener // set up clearJButton 233 clearJButton = new JButton(); 234 clearJButton.setBounds( 80, 214, 50, 50 ); 234 clearJButton.setText( "C" ); 236 contentPane.add( clearJButton ); 237 clearJButton.addActionListener( new ActionListener() // anonymous inner class 240 {
2004 Prentice Hall, Inc. All rights reserved. Outline 36 SecurityPanel.java (11 of 19) 241 // event handler called when clearJButton is pressed 242 public void actionPerformed( ActionEvent event ) 243 { 244 clearJButtonActionPerformed( event ); 245 } } // end anonymous inner class ); // end call to addActionListener // set up zeroJButton 252 zeroJButton = new JButton(); 253 zeroJButton.setBounds( 130, 214, 50, 50 ); 254 zeroJButton.setText( "0" ); 255 contentPane.add( zeroJButton ); 256 zeroJButton.addActionListener( new ActionListener() // anonymous inner class 259 { 260 // event handler called when zeroJButton is pressed 261 public void actionPerformed( ActionEvent event ) 262 { 263 zeroJButtonActionPerformed( event ); 264 } 265
2004 Prentice Hall, Inc. All rights reserved. Outline 37 SecurityPanel.java (11 of 19) 241 // event handler called when zeroJButton is pressed 242 public void actionPerformed( ActionEvent event ) 243 { 244 zeroJButtonActionPerformed( event ); 245 } } // end anonymous inner class ); // end call to addActionListener // set up clearJButton 252 clearJButton = new JButton(); 253 clearJButton.setBounds( 80, 214, 50, 50 ); 254 clearJButton.setText( "C" ); 255 contentPane.add( clearJButton ); 256 clearJButton.addActionListener( new ActionListener() // anonymous inner class 259 { 260 // event handler called when clearJButton is pressed 261 public void actionPerformed( ActionEvent event ) 262 { 263 clearJButtonActionPerformed( event ); 264 } 265
2004 Prentice Hall, Inc. All rights reserved. Outline 38 SecurityPanel.java (12 of 19) 266 } // end anonymous inner class ); // end call to addActionListener // set up enterJButton 271 enterJButton = new JButton(); 272 enterJButton.setBounds( 180, 214, 50, 50 ); 273 enterJButton.setText( "#" ); 274 contentPane.add( enterJButton ); 275 enterJButton.addActionListener( new ActionListener() // anonymous inner class 278 { 279 // event handler called when enterJButton is pressed 280 public void actionPerformed( ActionEvent event ) 281 { 282 enterJButtonActionPerformed( event ); 283 } } // end anonymous inner class ); // end call to addActionListener 288
2004 Prentice Hall, Inc. All rights reserved. Outline 39 SecurityPanel.java (13 of 19) 289 // set up accessLogJLabel 290 accessLogJLabel = new JLabel(); 291 accessLogJLabel.setBounds( 16, 285, 100, 16 ); 292 accessLogJLabel.setText( "Access log:" ); 293 contentPane.add( accessLogJLabel ); // set up accessLogJTextArea 296 accessLogJTextArea = new JTextArea(); // set up accessLogJScrollPane 299 accessLogJScrollPane = new JScrollPane( accessLogJTextArea ); 300 accessLogJScrollPane.setBounds( 16, 309, 270, 95 ); 301 contentPane.add( accessLogJScrollPane ); // set properties of application's window 304 setTitle( "Security Panel" ); // set window's title 305 setSize( 310, 450 ); // set window's size 306 setVisible( true ); // display window } // end method createUserInterface // append 1 to the security code 311 private void oneJButtonActionPerformed( ActionEvent event ) 312 {
2004 Prentice Hall, Inc. All rights reserved. Outline 40 SecurityPanel.java (14 of 19) 313 securityCodeJPasswordField.setText( String.valueOf( 314 securityCodeJPasswordField.getPassword() ) + "1" ); } // end method oneJButtonActionPerformed // append 2 to the security code 319 private void twoJButtonActionPerformed( ActionEvent event ) 320 { 321 securityCodeJPasswordField.setText( String.valueOf( 322 securityCodeJPasswordField.getPassword() ) + "2" ); } // end method twoJButtonActionPerformed // append 3 to the security code 327 private void threeJButtonActionPerformed( ActionEvent event ) 328 { 329 securityCodeJPasswordField.setText( String.valueOf( 330 securityCodeJPasswordField.getPassword() ) + "3" ); } // end method threeJButtonActionPerformed // append 4 to the security code 335 private void fourJButtonActionPerformed( ActionEvent event ) 336 { Append the numeric JButton value "1" to the password stored in the JPasswordField Append the numeric JButton value "2" to the password stored in the JPasswordField Append the numeric JButton value "3" to the password stored in the JPasswordField
2004 Prentice Hall, Inc. All rights reserved. Outline 41 SecurityPanel.java (15 of 19) 337 securityCodeJPasswordField.setText( String.valueOf( 338 securityCodeJPasswordField.getPassword() ) + "4" ); } // end method fourJButtonActionPerformed // append 5 to the security code 343 private void fiveJButtonActionPerformed( ActionEvent event ) 344 { 345 securityCodeJPasswordField.setText( String.valueOf( 346 securityCodeJPasswordField.getPassword() ) + "5" ); } // end method fiveJButtonActionPerformed // append 6 to the security code 351 private void sixJButtonActionPerformed( ActionEvent event ) 352 { 353 securityCodeJPasswordField.setText( String.valueOf( 354 securityCodeJPasswordField.getPassword() ) + "6" ); } // end method sixJButtonActionPerformed // append 7 to the security code 359 private void sevenJButtonActionPerformed( ActionEvent event ) 360 { Append the numeric JButton value "4" to the password stored in the JPasswordField Append the numeric JButton value "5" to the password stored in the JPasswordField Append the numeric JButton value "6" to the password stored in the JPasswordField
2004 Prentice Hall, Inc. All rights reserved. Outline 42 SecurityPanel.java (16 of 19) 361 securityCodeJPasswordField.setText( String.valueOf( 362 securityCodeJPasswordField.getPassword() ) + "7" ); } // end method sevenJButtonActionPerformed // append 8 to the security code 367 private void eightJButtonActionPerformed( ActionEvent event ) 368 { 369 securityCodeJPasswordField.setText( String.valueOf( 370 securityCodeJPasswordField.getPassword() ) + "8" ); } // end method eightJButtonActionPerformed // append 9 to the security code 375 private void nineJButtonActionPerformed( ActionEvent event ) 376 { 377 securityCodeJPasswordField.setText( String.valueOf( 378 securityCodeJPasswordField.getPassword() ) + "9" ); } // end method nineJButtonActionPerformed // append 0 to the security code 383 private void zeroJButtonActionPerformed( ActionEvent event ) 384 { Append the numeric JButton value "7" to the password stored in the JPasswordField Append the numeric JButton value "8" to the password stored in the JPasswordField Append the numeric JButton value "9" to the password stored in the JPasswordField
2004 Prentice Hall, Inc. All rights reserved. Outline 43 SecurityPanel.java (17 of 19) 385 securityCodeJPasswordField.setText( String.valueOf( 386 securityCodeJPasswordField.getPassword() ) + "0" ); } // end method zeroJButtonActionPerformed // clears the securityCodeJPasswordField 391 private void clearJButtonActionPerformed( ActionEvent event ) 392 { 393 securityCodeJPasswordField.setText( "" ); } // end method clearJButtonActionPerformed // gets access code and determines level of clearance 398 private void enterJButtonActionPerformed( ActionEvent event ) 399 { 400 String message; // displays access status of users // stores access code entered 403 int accessCode = Integer.parseInt( String.valueOf( 404 securityCodeJPasswordField.getPassword() ) ); securityCodeJPasswordField.setText( "" ); switch ( accessCode ) // check access code input 409 { Append the numeric JButton value "0" to the password stored in the JPasswordField Clear the JPasswordField Get password property Using a switch statement to determine user access level String for message to display Clear the JPasswordField
2004 Prentice Hall, Inc. All rights reserved. Outline 44 SecurityPanel.java (18 of 19) 410 // access code is 7, 8 or case 7: 412 case 8: 413 case 9: 414 message = "Restricted Access"; 415 break; // done processing case // access code equal to case 1645: 419 message = "Technician"; 420 break; // done processing case // access code equal to case 8345: 424 message = "Custodian"; 425 break; // done processing case // access code equal to 9998 or between 1006 and case 9998: 429 case 1006: 430 case 1007: 431 case 1008: 432 message = "Scientist"; 433 break; // done processing case 434 Three case labels result in the same message Terminate switch statement and continue execution with next statement after switch Access code 1645 for Technicians Access code 8345 for Custodians Access codes 9998, 1006, 1007, and 1008 for Scientists
2004 Prentice Hall, Inc. All rights reserved. Outline 45 SecurityPanel.java (19 of 19) 435 // if no other case is true 436 default: 437 message = "Access Denied"; } // end switch statement // display time and message in access log JTextArea 442 DateFormat formatter = DateFormat.getDateTimeInstance(); 443 accessLogJTextArea.append( formatter.format( new Date() ) " " + message + "\n" ); } // end method enterJButtonActionPerformed // main method 449 public static void main( String[] args ) 450 { 451 SecurityPanel application = new SecurityPanel(); 452 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class SecurityPanel Default case executes if no other case s match Right brace ends the switch statement Append the current date and time to the message