Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Java AWT Code Examples

ComboBox Demo

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
ComboBox Demo // 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 images; 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 ] ) }; public ComboBoxTest() { super( "Testing JComboBox" ); Container c = getContentPane(); c.setLayout( new FlowLayout() ); images = new JComboBox( names ); images.setMaximumRowCount( 3 ); images.addItemListener( new ItemListener() { public void itemStateChanged( ItemEvent e ) { label.setIcon( icons[ images.getSelectedIndex() ] ); } } ); c.add( images ); label = new JLabel( icons[ 0 ] ); c.add( label ); setSize( 350, 100 ); show(); } public static void main( String args[] ) { ComboBoxTest app = new ComboBoxTest(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } }