1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4
5 public class ComboBoxDemo extends JFrame {
6
7 private String[] flagTitles = {"Canada", "China", "Denmark",
8 "France", "Germany", "India", "Norway", "United Kingdom",
9 "United States of America"};
10
11
12 private ImageIcon[] flagImage = {
13 new ImageIcon("image/ca.gif"),
14 new ImageIcon("image/china.gif"),
15 new ImageIcon("image/denmark.gif"),
16 new ImageIcon("image/fr.gif"),
17 new ImageIcon("image/germany.gif"),
18 new ImageIcon("image/india.gif"),
19 new ImageIcon("image/norway.gif"),
20 new ImageIcon("image/uk.gif"),
21 new ImageIcon("image/us.gif")
22 };
23
24
25 private String[] flagDescription = new String[9];
26
27
28 private DescriptionPanel descriptionPanel = new DescriptionPanel();
29
30
31 private JComboBox<String> jcbo = new JComboBox<String>(flagTitles);
32
33 public static void main(String[] args) {
34 ComboBoxDemo frame = new ComboBoxDemo();
35 frame.pack();
36 frame.setTitle("ComboBoxDemo");
37 frame.setLocationRelativeTo(null);
38 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39 frame.setVisible(true);
40 }
41
42 public ComboBoxDemo() {
43
44 flagDescription[0] = "The Canadian national flag ...";
45 flagDescription[1] = "Description for China ... ";
46 flagDescription[2] = "Description for Denmark ... ";
47 flagDescription[3] = "Description for France ... ";
48 flagDescription[4] = "Description for Germany ... ";
49 flagDescription[5] = "Description for India ... ";
50 flagDescription[6] = "Description for Norway ... ";
51 flagDescription[7] = "Description for UK ... ";
52 flagDescription[8] = "Description for US ... ";
53
54
55 setDisplay(0);
56
57
58 add(jcbo, BorderLayout.NORTH);
59 add(descriptionPanel, BorderLayout.CENTER);
60
61
62 jcbo.addItemListener(new ItemListener() {
63 @Override
64 public void itemStateChanged(ItemEvent e) {
65 setDisplay(jcbo.getSelectedIndex());
66 }
67 });
68 }
69
70
71 public void setDisplay(int index) {
72 descriptionPanel.setTitle(flagTitles[index]);
73 descriptionPanel.setImageIcon(flagImage[index]);
74 descriptionPanel.setDescription(flagDescription[index]);
75 }
76 }