Text Field
Combo box
List
EVENT HANDLING
JTextField and
JPasswordField
• JTextField
– Single-line area in which user can enter text
• JPasswordField
– Extends JTextField
– Hides characters that user enters
1 // Fig. 7: [Link]
2 // Demonstrating the JTextField class.
3
4 // Java core packages
5 import [Link].*;
6 import [Link].*;
7
8 // Java extension packages
9 import [Link].*;
10
11 public class TextFieldTest extends JFrame {
12 private JTextField textField1, textField2, textField3;
13 private JPasswordField passwordField;
14
15 // set up GUI
16 public TextFieldTest()
17 {
18 super( "Testing JTextField and JPasswordField" );
19
20 Container container = getContentPane();
21 [Link]( new FlowLayout() );
22
23 // construct textfield with default sizing
24 textField1 = new JTextField( 10 );
25 [Link]( textField1 );
26
27 // construct textfield with default text
28 textField2 = new JTextField( "Enter text here" );
29 [Link]( textField2 );
30
31 // construct textfield with default text and
32 // 20 visible elements and no event handler
33 textField3 = new JTextField( "Uneditable text field", 20 );
34 [Link]( false );
35 [Link]( textField3 );
36
37 // construct textfield with default text
38 passwordField = new JPasswordField( "Hidden text" );
39 [Link]( passwordField );
40
41 // register event handlers
42 TextFieldHandler handler = new TextFieldHandler();
43 [Link]( handler );
44 [Link]( handler );
45 [Link]( handler );
46 [Link]( handler );
47
48 setSize( 325, 100 );
49 setVisible( true );
50 }
51
52 // execute application
53 public static void main( String args[] )
54 {
55 TextFieldTest application = new TextFieldTest();
56
57 [Link](
58 JFrame.EXIT_ON_CLOSE );
59 }
60
61 // private inner class for event handling
62 private class TextFieldHandler implements ActionListener {
63
64 // process text field events
65 public void actionPerformed( ActionEvent event )
66 {
67 String string = "";
68
69 // user pressed Enter in JTextField textField1
70 if ( [Link]() == textField1 )
71 string = "textField1: " + [Link]();
72
73 // user pressed Enter in JTextField textField2
74 else if ( [Link]() == textField2 )
75 string = "textField2: " + [Link]();
76
77 // user pressed Enter in JTextField textField3
78 else if ( [Link]() == textField3 )
79 string = "textField3: " + [Link]();
80
81 // user pressed Enter in JTextField passwordField
82 else if ( [Link]() == passwordField ) {
83 JPasswordField pwd =
84 ( JPasswordField ) [Link]();
85 string = "passwordField: " +
86 new String( [Link]() );
87 }
88
89 [Link]( null, string );
90 }
91
92 } // end private inner class TextFieldHandler
93
94 } // end class TextFieldTest
[Link]
Event registration for JTextField
textField1.
textField1 handler
This is the JTextField This is the TextFieldHandler
o bjec t. It c ontains a n o bjec t that imp lem ents
instanc e va riable o f typ e ActionListener and define s
EventListenerList m ethod actionPerformed.
c alled listenerListthat
it inherited from c lass public void
JComponent. actionPerformed(
ActionEvent event )
listenerList {
// event handled here
}
...
This refere nc e is c re ated by the stateme nt
[Link]( handler );
JCheckBox and JRadioButton
• State buttons
– On/Off or true/false values
– Java provides three types
• JToggleButton
• JCheckBox
• JRadioButton
3
4 // Java core packages
5 import [Link].*;
6 import [Link].*;
7 8 // Java extension packages
9 import [Link].*;
10
11 public class CheckBoxTest extends JFrame {
12 private JTextField field;
13 private JCheckBox bold, italic;
14
15 // set up GUI
16 public CheckBoxTest()
17 {
18 super( "JCheckBox Test" );
19
20 // get content pane and set its layout
21 Container container = getContentPane();
22 [Link]( new FlowLayout() );
23
24 // set up JTextField and set its font
25 field =
26 new JTextField( "Watch the font style change", 20 );
27 [Link]( new Font( "Serif", [Link], 14 ) );
28 [Link]( field );
29
30 // create checkbox objects
31 bold = new JCheckBox( "Bold" );
32 [Link]( bold );
33
34 italic = new JCheckBox( "Italic" );
35 [Link]( italic );
36
37 // register listeners for JCheckBoxes
38 CheckBoxHandler handler = new CheckBoxHandler();
39 [Link]( handler );
40 [Link]( handler );
41
42 setSize( 275, 100 );
43 setVisible( true );
44 }
45
46 // execute application
47 public static void main( String args[] )
48 {
49 CheckBoxTest application = new CheckBoxTest();
50
51 [Link](
52 JFrame.EXIT_ON_CLOSE );
53 }
54
55 // private inner class for ItemListener event handling
56 private class CheckBoxHandler implements ItemListener {
57 private int valBold = [Link];
58 private int valItalic = [Link];
59
60 // respond to checkbox events
61 public void itemStateChanged( ItemEvent event )
62 {
63 // process bold checkbox events
64 if ( [Link]() == bold )
65
66 if ( [Link]() == [Link] )
67 valBold = [Link];
68 else
69 valBold = [Link];
70
• 71 // process italic checkbox events
72 if ( [Link]() == italic )
73
74 if ( [Link]() == [Link] )
75 valItalic = [Link];
76 else
77 valItalic = [Link];
78
79 // set text field font
80 [Link](
81 new Font( "Serif", valBold + valItalic, 14 ) );
82 }
83
84 } // end private inner class CheckBoxHandler
85
86 } // end class CheckBoxTest
JList
• List
– Series of items
– user can select one or more items
– Single-selection vs. multiple-selection
– JList
36
37 // do not allow multiple selections
38 [Link](
39 ListSelectionModel.SINGLE_SELECTION ); JList allows single
40
41 // add a JScrollPane containing JList to content pane selections
42 [Link]( new JScrollPane( colorList ) );
43
44 // set up event handler Register JList to receive events from
45 [Link](
46 anonymous
47 // anonymous inner class for list selection events ListSelectionListener
48 new ListSelectionListener() {
49
50 // handle list selection events
51 public void valueChanged( ListSelectionEvent event )
52 {
53 [Link](
54 colors[ [Link]() ] );
55 }
56
57 } // end anonymous inner class
58
When user selects item in
59 ); // end call to addListSelectionListener JList,
60
61 setSize( 350, 150 );
ListSelectionListener
62 setVisible( true ); invokes method
63 }
64
valueChanged of all
65 // execute application Set appropriate
registered listeners
66 public static void main( String args[] ) background depending on
67 {
68 ListTest application = new ListTest(); user selection
69
70 [Link](
71 JFrame.EXIT_ON_CLOSE );
72 }
73
74 } // end class ListTest