java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3942
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to change the look and feel of Swing applications E-mail
User Rating: / 37
PoorBest 
Swing Look and Feel

This Java tip illustrates a method of changing the look and feel of your application. This code inlcudes a listener that can change the look-and-feel of a frame based on the actionCommand of an ActionEvent object. Supported look-and-feels are: Metal, Motif, Windows. Not all L&Fs will be available on a given machine. Notably, the Windows L&Fs work only on its platform.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LookAndFeelDemo extends JFrame {

   private String strings[] "Metal""Motif""Windows" };
   private UIManager.LookAndFeelInfo looks[];
   private JRadioButton radio[];
   private ButtonGroup group;
   private JButton button;
   private JLabel label;
   private JComboBox comboBox;

   public LookAndFeelDemo()
   {
      super"Look and Feel Demo" );

      Container c = getContentPane();

      JPanel northPanel = new JPanel();
      northPanel.setLayoutnew GridLayout310) );
      label = new JLabel"This is a Metal look-and-feel",
                          SwingConstants.CENTER );
      northPanel.addlabel );
      button = new JButton"JButton" );
      northPanel.addbutton );
      comboBox = new JComboBoxstrings );
      northPanel.addcomboBox );
     
      c.addnorthPanel, BorderLayout.NORTH );
      
      JPanel southPanel = new JPanel();
      radio = new JRadioButtonstrings.length ];
      group = new ButtonGroup();
      ItemHandler handler = new ItemHandler();
      southPanel.setLayout(
         new GridLayout1, radio.length ) );

      for int i = 0; i < radio.length; i++ ) {
         radionew JRadioButtonstrings] );
         radio].addItemListenerhandler );
         group.addradio] );
         southPanel.addradio] );
      }

      c.addsouthPanel, BorderLayout.SOUTH );

      // get the installed look-and-feel information
      looks = UIManager.getInstalledLookAndFeels();

      setSize300200 );
      show();

      radio].setSelectedtrue );
   }

   private void changeTheLookAndFeelint value )
   {
      try {
         UIManager.setLookAndFeel(
            looksvalue ].getClassName() );
         SwingUtilities.updateComponentTreeUIthis );
      }
      catch Exception e ) {
         e.printStackTrace();
      }
   }

   public static void mainString args[] )
   {
      LookAndFeelDemo dx = new LookAndFeelDemo();

      dx.addWindowListener(
         new WindowAdapter() {
            public void windowClosingWindowEvent e )
            {
               System.exit);
            }
         }
      );
   }

   private class ItemHandler implements ItemListener {
      public void itemStateChangedItemEvent e )
      {
         for int i = 0; i < radio.length; i++ )
            if radio].isSelected() ) {
               label.setText"This is a " +
                  strings" look-and-feel" );
               comboBox.setSelectedIndex);
               changeTheLookAndFeel);
            }
      }
   }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.