Change Look And Feel of Components Dynamically in Swing

Mac OS X Lion
Most of the people might knew about the UIManagerclass and the setLookAndFeel() method in it. But the UI doesn't update when this method is called upon user's request. There is definitely something that is needed to be done to let all the components get the look and feel. Here is a full code on how to dynamically change the look and feel upon user's request.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class LookAndFeelDemo1 extends JFrame implements ActionListener
{
JMenuBar mbar;
JMenu menu;
JRadioButtonMenuItem metal,nimbus,windows,windowsClassic,motif;
ButtonGroup bg;

    public LookAndFeelDemo1()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        // Set frame properties
        setTitle("Look And Feel Demo");
        setLayout(new FlowLayout());
        setSize(400,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        // Create a JMenuBar
        mbar=new JMenuBar();
       
        // Create a JMenu
        menu=new JMenu("Look And Feel");
       
        // Create some JRadioButtonMenuItems
        metal=new JRadioButtonMenuItem("javax.swing.plaf.metal.MetalLookAndFeel");
        nimbus=new JRadioButtonMenuItem("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        windows=new JRadioButtonMenuItem("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        windowsClassic=new JRadioButtonMenuItem("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
        motif=new JRadioButtonMenuItem("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
       
        // Add ActionListeners to all
        // You can also add ItemListener
        metal.addActionListener(this);
        nimbus.addActionListener(this);
        windows.addActionListener(this);
        windowsClassic.addActionListener(this);
        motif.addActionListener(this);
       
        // Create a ButtonGroup
        bg=new ButtonGroup();
       
        // Add all the JRadioButtonMenuItems to bg
        bg.add(metal);
        bg.add(nimbus);
        bg.add(windows);
        bg.add(windowsClassic);
        bg.add(motif);
       
       
        // Add the JRadioButtonMenuItems to menu
        menu.add(metal);
        menu.add(nimbus);
        menu.add(windows);
        menu.add(windowsClassic);
        menu.add(motif);
       
        // Add JMenu to mbar
        mbar.add(menu);
       
        // Set the JMenuBar
        setJMenuBar(mbar);
       
        // Add some components
        add(new JButton("Button"));
        add(new JTextField(20));
        add(new JLabel("Label"));
        add(new JCheckBox("Checkbox"));
    }
   
    public void actionPerformed(ActionEvent ae)
    {
        try
        {
            // Get the source of the event
            // Here a JRadioButtonMenuItem
            JRadioButtonMenuItem m=(JRadioButtonMenuItem)ae.getSource();
           
            // Set the look and feel
            UIManager.setLookAndFeel(m.getText());
           
            // Update all the components. This method takes
            // the JRootPane and calls updateUI() method on every component
            // associated with the given root pane
            // This is the method that updates the look and feel of
            // every component
            SwingUtilities.updateComponentTreeUI(getRootPane());
        }catch(Exception e){}
    }
   
    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable(){
            public void run()
            {
                new LookAndFeelDemo1();
            }
        });
    }
}

In this way, we can easily update the look and feel of components dynamically upon user's request. Hope you enjoyed, share if you like.


Posted by at on

Tags: Swing Hacks,