-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathComboBoxTest.java
52 lines (47 loc) · 1.61 KB
/
ComboBoxTest.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
// Fig. 13.13: ComboBoxTest.java
// Using a JComboBox to select an image to display.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBoxTest extends JFrame {
private JComboBox imagesComboBox;
private JLabel label;
private String names[] = { "bug1.jpeg", "bug2.png", "bug3.jpeg", "bug4.jpeg" };
private Icon icons[] = { new ImageIcon( names[ 0 ] ),
new ImageIcon( names[ 1 ] ), new ImageIcon( names[ 2 ] ),
new ImageIcon( names[ 3 ] ) };
// set up GUI
public ComboBoxTest()
{
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(
new ItemListener() { // anonymous inner class
// 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 );
} // end ComboBoxTest constructor
public static void main( String args[] )
{
ComboBoxTest application = new ComboBoxTest();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class ComboBoxTest