JTextFields and JPasswordFields.

uses classes JTextField and JPasswordField to create and
manipulate four text fields. When the user types in one
of the text fields, then presses Enter, the application
displays a message dialog box containing the text the user
typed. You can only type in the text field that is "in
focus." A component receives the focus when the user clicks
the component. This is important because the text field with
the focus is the one that generates an event when the user
presses Enter. In this example, when the user presses Enter
in the JPasswordField, the password is revealed. We begin by
discussing the setup of the GUI, then discuss the
event-handling code.

 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 text field 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

Test class for TextFieldFrame.
1  // Fig. 11.10: TextFieldTest.java
2  // Testing TextFieldFrame.
3  import javax.swing.JFrame;
4
5  public class TextFieldTest
6  {
7     public static void main( String args[] )
8     {
9        TextFieldFrame textFieldFrame = new TextFieldFrame();
10        textFieldFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
11        textFieldFrame.setSize( 325, 100 ); // set frame size
12        textFieldFrame.setVisible( true ); // display frame
13     } // end main
14  } // end class TextFieldTest

One thought on “JTextFields and JPasswordFields.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s