-
Notifications
You must be signed in to change notification settings - Fork 0
/
JCombo_Box_Test_1.java
71 lines (52 loc) · 1.96 KB
/
JCombo_Box_Test_1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package jcombo_box_test_1;
// Java core packages
import java.awt.*;
import java.awt.event.*;
// Java extension packages
import javax.swing.*;
public class JCombo_Box_Test_1 extends JFrame {
private JComboBox imagesComboBox;
private JLabel label;
private String names[] =
{ "bug1.gif", "bug2.gif", "travelbug.gif", "buganim.gif" };
private Icon icons[] = { new ImageIcon( names[ 0 ] ),
new ImageIcon( names[ 1 ] ), new ImageIcon( names[ 2 ] ),
new ImageIcon( names[ 3 ] ) };
// set up GUI
public JCombo_Box_Test_1()
{
super( "Testing JComboBox" );
// get content pane and set its layout
Container container = getContentPane();
container.setLayout( new FlowLayout() );
// set up JComboBox and register its event handler
imagesComboBox = new JComboBox( names );
imagesComboBox.setMaximumRowCount( 3 );
imagesComboBox.addItemListener(
// anonymous inner class to handle JComboBox events
new ItemListener() {
// handle JComboBox event
public void itemStateChanged( ItemEvent event )
{
// determine whether check box selected
if ( event.getStateChange() == ItemEvent.SELECTED )
label.setIcon( icons[
imagesComboBox.getSelectedIndex() ] );
}
} // end anonymous inner class
); // end call to addItemListener
container.add( imagesComboBox );
// set up JLabel to display ImageIcons
label = new JLabel( icons[ 0 ] );
container.add( label );
setSize( 350, 100 );
setVisible( true );
}
// execute application
public static void main( String args[] )
{
JCombo_Box_Test_1 application = new JCombo_Box_Test_1();
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
}
} // end class ComboBoxTest