// Purpose. JComboBox demo import javax.swing.*; import java.awt.event.*; public class JComboBoxDemo { public static JPanel createComboBox( String[] choices ) { JPanel comPanel = new JPanel(); JComboBox combo = new JComboBox( choices ); comPanel.add( combo ); ItemListener cl = new ItemListener() { public void itemStateChanged( ItemEvent e ) { System.out.println( e.getItem() + " is " + ((e.getStateChange() == ItemEvent.SELECTED) ? "on" : "off") ); } }; combo.addItemListener( cl ); return comPanel; } public static void main( String[] args ) { JFrame frame = new JFrame( "JComboBox demo" ); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); String[] choices = { "Alpha","Bravo","Charlie","Delta","Echo","Foxtrot" }; frame.getContentPane().add( createComboBox( choices ) ); frame.setSize( 300, 240 ); frame.setVisible( true ); } } // Echo is on // Echo is off // Bravo is on // Bravo is off // Foxtrot is on