Source of TextFieldFrame.java


  1: // Fig. 11.9: TextFieldFrame.java
  2: // Demonstrating the JTextField class.
  3: import java.awt.FlowLayout;
  4: import java.awt.event.ActionListener;
  5: import java.awt.event.ActionEvent;
  6: import javax.swing.JFrame;
  7: import javax.swing.JTextField;
  8: import javax.swing.JPasswordField;
  9: import javax.swing.JOptionPane;
 10: 
 11: public class TextFieldFrame extends JFrame 
 12: {
 13:    private JTextField textField1; // text field with set size
 14:    private JTextField textField2; // text field constructed with text
 15:    private JTextField textField3; // text field with text and size
 16:    private JPasswordField passwordField; // password field with text
 17: 
 18:    // TextFieldFrame constructor adds JTextFields to JFrame
 19:    public TextFieldFrame()
 20:    {
 21:       super( "Testing JTextField and JPasswordField" );
 22:       setLayout( new FlowLayout() ); // set frame layout
 23: 
 24:       // construct textfield with 10 columns
 25:       textField1 = new JTextField( 10 ); 
 26:       add( textField1 ); // add textField1 to JFrame
 27: 
 28:       // construct textfield with default text
 29:       textField2 = new JTextField( "Enter text here" );
 30:       add( textField2 ); // add textField2 to JFrame
 31: 
 32:       // construct textfield with default text and 21 columns
 33:       textField3 = new JTextField( "Uneditable text field", 21 );
 34:       textField3.setEditable( false ); // disable editing
 35:       add( textField3 ); // add textField3 to JFrame
 36: 
 37:       // construct passwordfield with default text
 38:       passwordField = new JPasswordField( "Hidden text" );
 39:       add( passwordField ); // add passwordField to JFrame
 40: 
 41:       // register event handlers
 42:       TextFieldHandler handler = new TextFieldHandler();
 43:       textField1.addActionListener( handler );
 44:       textField2.addActionListener( handler );
 45:       textField3.addActionListener( handler );
 46:       passwordField.addActionListener( handler );
 47:    } // end TextFieldFrame constructor
 48: 
 49:    // private inner class for event handling
 50:    private class TextFieldHandler implements ActionListener 
 51:    {
 52:       // process textfield events
 53:       public void actionPerformed( ActionEvent event )
 54:       {
 55:          String string = ""; // declare string to display
 56: 
 57:          // user pressed Enter in JTextField textField1
 58:          if ( event.getSource() == textField1 )
 59:             string = String.format( "textField1: %s",
 60:                event.getActionCommand() );
 61: 
 62:          // user pressed Enter in JTextField textField2
 63:          else if ( event.getSource() == textField2 )
 64:             string = String.format( "textField2: %s",
 65:                event.getActionCommand() );
 66: 
 67:          // user pressed Enter in JTextField textField3
 68:          else if ( event.getSource() == textField3 )
 69:             string = String.format( "textField3: %s", 
 70:                event.getActionCommand() );
 71: 
 72:          // user pressed Enter in JTextField passwordField
 73:          else if ( event.getSource() == passwordField )
 74:             string = String.format( "passwordField: %s", 
 75:                new String( passwordField.getPassword() ) );
 76: 
 77:          // display JTextField content
 78:          JOptionPane.showMessageDialog( null, string ); 
 79:       } // end method actionPerformed
 80:    } // end private inner class TextFieldHandler
 81: } // end class TextFieldFrame
 82: 
 83: /**************************************************************************
 84:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 85:  * Pearson Education, Inc. All Rights Reserved.                           *
 86:  *                                                                        *
 87:  * DISCLAIMER: The authors and publisher of this book have used their     *
 88:  * best efforts in preparing the book. These efforts include the          *
 89:  * development, research, and testing of the theories and programs        *
 90:  * to determine their effectiveness. The authors and publisher make       *
 91:  * no warranty of any kind, expressed or implied, with regard to these    *
 92:  * programs or to the documentation contained in these books. The authors *
 93:  * and publisher shall not be liable in any event for incidental or       *
 94:  * consequential damages in connection with, or arising out of, the       *
 95:  * furnishing, performance, or use of these programs.                     *
 96:  *************************************************************************/