0% found this document useful (0 votes)
36 views14 pages

Text Field Combo Box List Event Handling

This document discusses Java Swing components for displaying lists (JList) and handling list selection events. It shows how to create a JList with selectable color names, register an anonymous ListSelectionListener to handle selection changes, and update the background color based on the selected item. When the user selects an item in the JList, the listener's valueChanged method is invoked to set the appropriate background color.

Uploaded by

Syeda rahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views14 pages

Text Field Combo Box List Event Handling

This document discusses Java Swing components for displaying lists (JList) and handling list selection events. It shows how to create a JList with selectable color names, register an anonymous ListSelectionListener to handle selection changes, and update the background color based on the selected item. When the user selects an item in the JList, the listener's valueChanged method is invoked to set the appropriate background color.

Uploaded by

Syeda rahi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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: TextFieldTest.java
2 // Demonstrating the JTextField class.
3
4 // Java core packages
5 import java.awt.*;
6 import java.awt.event.*;
7
8 // Java extension packages
9 import javax.swing.*;
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 container.setLayout( new FlowLayout() );
22
23 // construct textfield with default sizing
24 textField1 = new JTextField( 10 );
25 container.add( textField1 );
26
27 // construct textfield with default text
28 textField2 = new JTextField( "Enter text here" );
29 container.add( 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 textField3.setEditable( false );
35 container.add( textField3 );
36
37 // construct textfield with default text
38 passwordField = new JPasswordField( "Hidden text" );
39 container.add( passwordField );
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
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 application.setDefaultCloseOperation(
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 ( event.getSource() == textField1 )
71 string = "textField1: " + event.getActionCommand();
72
73 // user pressed Enter in JTextField textField2
74 else if ( event.getSource() == textField2 )
75 string = "textField2: " + event.getActionCommand();
76
77 // user pressed Enter in JTextField textField3
78 else if ( event.getSource() == textField3 )
79 string = "textField3: " + event.getActionCommand();
80
81 // user pressed Enter in JTextField passwordField
82 else if ( event.getSource() == passwordField ) {
83 JPasswordField pwd =
84 ( JPasswordField ) event.getSource();
85 string = "passwordField: " +
86 new String( passwordField.getPassword() );
87 }
88
89 JOptionPane.showMessageDialog( null, string );
90 }
91
92 } // end private inner class TextFieldHandler
93
94 } // end class TextFieldTest
TextFieldTest.java
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


textField1.addActionListener( 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 java.awt.*;
6 import java.awt.event.*;
7 8 // Java extension packages
9 import javax.swing.*;
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 container.setLayout( new FlowLayout() );
23
24 // set up JTextField and set its font
25 field =
26 new JTextField( "Watch the font style change", 20 );
27 field.setFont( new Font( "Serif", Font.PLAIN, 14 ) );
28 container.add( field );
29
30 // create checkbox objects
31 bold = new JCheckBox( "Bold" );
32 container.add( bold );
33
34 italic = new JCheckBox( "Italic" );
35 container.add( italic );
36
37 // register listeners for JCheckBoxes
38 CheckBoxHandler handler = new CheckBoxHandler();
39 bold.addItemListener( handler );
40 italic.addItemListener( 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 application.setDefaultCloseOperation(
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 = Font.PLAIN;
58 private int valItalic = Font.PLAIN;
59
60 // respond to checkbox events
61 public void itemStateChanged( ItemEvent event )
62 {
63 // process bold checkbox events
64 if ( event.getSource() == bold )
65
66 if ( event.getStateChange() == ItemEvent.SELECTED )
67 valBold = Font.BOLD;
68 else
69 valBold = Font.PLAIN;
70
• 71 // process italic checkbox events
72 if ( event.getSource() == italic )
73
74 if ( event.getStateChange() == ItemEvent.SELECTED )
75 valItalic = Font.ITALIC;
76 else
77 valItalic = Font.PLAIN;
78
79 // set text field font
80 field.setFont(
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 colorList.setSelectionMode(
39 ListSelectionModel.SINGLE_SELECTION ); JList allows single
40
41 // add a JScrollPane containing JList to content pane selections
42 container.add( new JScrollPane( colorList ) );
43
44 // set up event handler Register JList to receive events from
45 colorList.addListSelectionListener(
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 container.setBackground(
54 colors[ colorList.getSelectedIndex() ] );
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 application.setDefaultCloseOperation(
71 JFrame.EXIT_ON_CLOSE );
72 }
73
74 } // end class ListTest

You might also like