Source of ComboBoxFrame.java


  1: // Fig. 11.21: ComboBoxFrame.java
  2: // Using a JComboBox to select an image to display.
  3: import java.awt.FlowLayout;
  4: import java.awt.event.ItemListener;
  5: import java.awt.event.ItemEvent;
  6: import javax.swing.JFrame;
  7: import javax.swing.JLabel;
  8: import javax.swing.JComboBox;
  9: import javax.swing.Icon;
 10: import javax.swing.ImageIcon;
 11: 
 12: public class ComboBoxFrame extends JFrame 
 13: {
 14:    private JComboBox imagesJComboBox; // combobox to hold names of icons
 15:    private JLabel label; // label to display selected icon
 16: 
 17:    private String names[] = 
 18:       { "bug1.gif", "bug2.gif",  "travelbug.gif", "buganim.gif" };
 19:    private Icon icons[] = { 
 20:       new ImageIcon( getClass().getResource( names[ 0 ] ) ),
 21:       new ImageIcon( getClass().getResource( names[ 1 ] ) ), 
 22:       new ImageIcon( getClass().getResource( names[ 2 ] ) ),
 23:       new ImageIcon( getClass().getResource( names[ 3 ] ) ) };
 24: 
 25:    // ComboBoxFrame constructor adds JComboBox to JFrame
 26:    public ComboBoxFrame()
 27:    {
 28:       super( "Testing JComboBox" );
 29:       setLayout( new FlowLayout() ); // set frame layout     
 30: 
 31:       imagesJComboBox = new JComboBox( names ); // set up JComboBox
 32:       imagesJComboBox.setMaximumRowCount( 3 ); // display three rows
 33: 
 34:       imagesJComboBox.addItemListener(
 35:          new ItemListener() // anonymous inner class
 36:          {
 37:             // handle JComboBox event
 38:             public void itemStateChanged( ItemEvent event )
 39:             {
 40:                // determine whether check box selected
 41:                if ( event.getStateChange() == ItemEvent.SELECTED )
 42:                   label.setIcon( icons[ 
 43:                      imagesJComboBox.getSelectedIndex() ] );
 44:             } // end method itemStateChanged
 45:          } // end anonymous inner class
 46:       ); // end call to addItemListener
 47: 
 48:       add( imagesJComboBox ); // add combobox to JFrame
 49:       label = new JLabel( icons[ 0 ] ); // display first icon
 50:       add( label ); // add label to JFrame
 51:    } // end ComboBoxFrame constructor
 52: } // end class ComboBoxFrame
 53: 
 54: 
 55: /**************************************************************************
 56:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 57:  * Pearson Education, Inc. All Rights Reserved.                           *
 58:  *                                                                        *
 59:  * DISCLAIMER: The authors and publisher of this book have used their     *
 60:  * best efforts in preparing the book. These efforts include the          *
 61:  * development, research, and testing of the theories and programs        *
 62:  * to determine their effectiveness. The authors and publisher make       *
 63:  * no warranty of any kind, expressed or implied, with regard to these    *
 64:  * programs or to the documentation contained in these books. The authors *
 65:  * and publisher shall not be liable in any event for incidental or       *
 66:  * consequential damages in connection with, or arising out of, the       *
 67:  * furnishing, performance, or use of these programs.                     *
 68:  *************************************************************************/