• Source
    1. // Fig. 14.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. // TextFieldFrame constructor adds JTextFields to JFrame
    18. public TextFieldFrame()
    19. {
    20. super( "Testing JtextField and JPasswordField" );
    21. setLayout( new FlowLayout() ); // set frame layout
    22. // construct textfield with 10 columns
    23.  
    24. textField1 = new JTextField( 10 );
    25. add( textField1 ); // add textField1 to JFrame
    26. // construct textfield with default text
    27. textField2 = new JTextField( "Enter text here" );
    28. add( textField2 ); // add textField2 to JFrame
    29. // construct textfield with default text and 21 columns
    30. textField3 = new JTextField( "Uneditable text field", 21 );
    31. textField3.setEditable( false ); // disable editing
    32. add( textField3 ); // add textField3 to JFrame
    33. // construct passwordfield with default text
    34. passwordField = new JPasswordField( "Hidden text" );
    35. add( passwordField ); // add passwordField to JFrame
    36. // register event handlers
    37. TextFieldHandler handler = new TextFieldHandler();
    38. textField1.addActionListener( handler );
    39. textField2.addActionListener( handler );
    40. textField3.addActionListener( handler );
    41. passwordField.addActionListener( handler );
    42. } // end TextFieldFrame constructor
    43. // private inner class for event handling
    44.  
    45. private class TextFieldHandler implements ActionListener
    46. {
    47. // process text field events
    48. public void actionPerformed( ActionEvent event )
    49. {
    50. String string = ""; // declare string to display
    51. // user pressed Enter in JTextField textField1
    52. if ( event.getSource() == textField1 )
    53. string = String.format( "textField1: %s",
    54. event.getActionCommand() );
    55. // user pressed Enter in JTextField textField2
    56. else if ( event.getSource() == textField2 )
    57. string = String.format( "textField2: %s",
    58. event.getActionCommand() );
    59. // user pressed Enter in JTextField textField3
    60. else if ( event.getSource() == textField3 )
    61. string = String.format( "textField3: %s",
    62. event.getActionCommand() );
    63. // user pressed Enter in JTextField passwordField
    64. else if ( event.getSource() == passwordField )
    65. string = String.format( "passwordField: %s",
    66. event.getActionCommand() );
    67. // display JTextField content
    68. JOptionPane.showMessageDialog( null, string );
    69. } // end method actionPerformed
    70. } // end private inner class TextFieldHandler
    71.  
    72. public static void main( String[] args )
    73. {
    74. TextFieldFrame textFieldFrame = new TextFieldFrame();
    75. textFieldFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    76. textFieldFrame.setSize( 350, 100 ); // set frame size
    77. textFieldFrame.setVisible( true );
    78. }
    79.  
    80. } // end class TextFieldFrame
    81.