Skip to main content

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.

Comments

Popular posts from this blog

Setting Background Image in JFrame - Swing

Here is sample tutorial, a simple trick that enables you to set background image for JFrame.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
{
JButton b1;
JLabel l1;
public BackgroundImageJFrame()
{
setTitle("Background Color for JFrame");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
/*
One way
-----------------
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
add(background);
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
background.add(l1);
background.add(b1);
*/
// Another way
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am …

Add two numbers in JSP easily

Here is an example on adding up two numbers in JSP. This is very simple as you see below. This requires very small piece of code. All i have done here is created a HTML file containing the input fields which contain the numbers to add up.Now the values in the input field are of type String, so i have done type casting and converted them into int using Integer.parseInt(). See how easy it looks.



Catch The Rat: Simple Game in Java

A simple, shortest and easiest game that could be written in Java using Swing components and event handling.