1  import java.awt.*;
  2  import java.awt.event.*;
  3  import javax.swing.*;
  4  
  5  public class ComboBoxDemo extends JFrame {
  6    // Declare an array of Strings for flag titles
  7    private String[] flagTitles = {"Canada", "China", "Denmark",
  8      "France", "Germany", "India", "Norway", "United Kingdom",
  9      "United States of America"};
 10  
 11    // Declare an ImageIcon array for the national flags of 9 countries
 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    // Declare an array of strings for flag descriptions
 25    private String[] flagDescription = new String[9];
 26  
 27    // Declare and create a description panel
 28    private DescriptionPanel descriptionPanel = new DescriptionPanel();
 29  
 30    // Create a combo box for selecting countries
 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); // Center the frame
 38      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 39      frame.setVisible(true);
 40    }
 41  
 42    public ComboBoxDemo() {
 43      // Set text description
 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      // Set the first country (Canada) for display
 55      setDisplay(0);
 56  
 57      // Add combo box and description panel to the frame
 58      add(jcbo, BorderLayout.NORTH);
 59      add(descriptionPanel, BorderLayout.CENTER);
 60  
 61      // Register listener
 62      jcbo.addItemListener(new ItemListener() {
 63        @Override /** Handle item selection */
 64        public void itemStateChanged(ItemEvent e) {
 65          setDisplay(jcbo.getSelectedIndex());
 66        }
 67      });
 68    }
 69  
 70    /** Set display information on the description panel */
 71    public void setDisplay(int index) {
 72      descriptionPanel.setTitle(flagTitles[index]);
 73      descriptionPanel.setImageIcon(flagImage[index]);
 74      descriptionPanel.setDescription(flagDescription[index]);
 75    }
 76  }