Source of LookAndFeelFrame.java


  1: // Fig. 22.17: LookAndFeelFrame.java
  2: // Changing the look and feel.
  3: import java.awt.GridLayout;
  4: import java.awt.BorderLayout;
  5: import java.awt.event.ItemListener;
  6: import java.awt.event.ItemEvent;
  7: import javax.swing.JFrame;
  8: import javax.swing.UIManager;
  9: import javax.swing.JRadioButton;
 10: import javax.swing.ButtonGroup;
 11: import javax.swing.JButton;
 12: import javax.swing.JLabel;
 13: import javax.swing.JComboBox;
 14: import javax.swing.JPanel;
 15: import javax.swing.SwingConstants;
 16: import javax.swing.SwingUtilities;
 17: 
 18: public class LookAndFeelFrame extends JFrame 
 19: {
 20:    // string names of look and feels
 21:    private final String strings[] = { "Metal", "Motif", "Windows" };
 22:    private UIManager.LookAndFeelInfo looks[]; // look and feels
 23:    private JRadioButton radio[]; // radiobuttons to select look and feel
 24:    private ButtonGroup group; // group for radiobuttons
 25:    private JButton button; // displays look of button
 26:    private JLabel label; // displays look of label
 27:    private JComboBox comboBox; // displays look of combo box
 28: 
 29:    // set up GUI
 30:    public LookAndFeelFrame()
 31:    {
 32:       super( "Look and Feel Demo" );
 33: 
 34:       JPanel northPanel = new JPanel(); // create north panel
 35:       northPanel.setLayout( new GridLayout( 3, 1, 0, 5 ) );
 36: 
 37:       label = new JLabel( "This is a Metal look-and-feel",
 38:          SwingConstants.CENTER ); // create label
 39:       northPanel.add( label ); // add label to panel
 40: 
 41:       button = new JButton( "JButton" ); // create button
 42:       northPanel.add( button ); // add button to panel
 43: 
 44:       comboBox = new JComboBox( strings ); // create combobox
 45:       northPanel.add( comboBox ); // add combobox to panel
 46:      
 47:       // create array for radio buttons
 48:       radio = new JRadioButton[ strings.length ];
 49: 
 50:       JPanel southPanel = new JPanel(); // create south panel
 51:       southPanel.setLayout( new GridLayout( 1, radio.length ) );
 52: 
 53:       group = new ButtonGroup(); // button group for look and feels
 54:       ItemHandler handler = new ItemHandler(); // look and feel handler
 55: 
 56:       for ( int count = 0; count < radio.length; count++ ) 
 57:       {
 58:          radio[ count ] = new JRadioButton( strings[ count ] );
 59:          radio[ count ].addItemListener( handler ); // add handler
 60:          group.add( radio[ count ] ); // add radiobutton to group
 61:          southPanel.add( radio[ count ] ); // add radiobutton to panel
 62:       } // end for
 63: 
 64:       add( northPanel, BorderLayout.NORTH ); // add north panel
 65:       add( southPanel, BorderLayout.SOUTH ); // add south panel
 66: 
 67:       // get installed look-and-feel information
 68:       looks = UIManager.getInstalledLookAndFeels();
 69:       radio[ 0 ].setSelected( true ); // set default selection
 70:    } // end LookAndFeelFrame constructor
 71: 
 72:    // use UIManager to change look-and-feel of GUI
 73:    private void changeTheLookAndFeel( int value )
 74:    {
 75:       try // change look and feel
 76:       {
 77:          // set look and feel for this application
 78:          UIManager.setLookAndFeel( looks[ value ].getClassName() );
 79: 
 80:          // update components in this application
 81:          SwingUtilities.updateComponentTreeUI( this );
 82:       } // end try
 83:       catch ( Exception exception ) 
 84:       {
 85:          exception.printStackTrace();
 86:       } // end catch
 87:    } // end method changeTheLookAndFeel
 88: 
 89:    // private inner class to handle radio button events
 90:    private class ItemHandler implements ItemListener 
 91:    {
 92:       // process user's look-and-feel selection
 93:       public void itemStateChanged( ItemEvent event )
 94:       {
 95:          for ( int count = 0; count < radio.length; count++ )
 96:          {
 97:             if ( radio[ count ].isSelected() ) 
 98:             {
 99:                label.setText( String.format( "This is a %s look-and-feel", 
100:                   strings[ count ] ) );
101:                comboBox.setSelectedIndex( count ); // set combobox index
102:                changeTheLookAndFeel( count ); // change look and feel
103:             } // end if
104:          } // end for
105:       } // end method itemStateChanged
106:    } // end private inner class ItemHandler
107: } // end class LookAndFeelFrame
108: 
109: 
110: 
111: /**************************************************************************
112:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
113:  * Pearson Education, Inc. All Rights Reserved.                           *
114:  *                                                                        *
115:  * DISCLAIMER: The authors and publisher of this book have used their     *
116:  * best efforts in preparing the book. These efforts include the          *
117:  * development, research, and testing of the theories and programs        *
118:  * to determine their effectiveness. The authors and publisher make       *
119:  * no warranty of any kind, expressed or implied, with regard to these    *
120:  * programs or to the documentation contained in these books. The authors *
121:  * and publisher shall not be liable in any event for incidental or       *
122:  * consequential damages in connection with, or arising out of, the       *
123:  * furnishing, performance, or use of these programs.                     *
124:  *************************************************************************/