Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / Java
Tip/Trick

Java Swing Look and Feel Picker

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Sep 2012CPOL 7.8K   1  
A really simple look and feel picker

Introduction

Just a really simple window that allows you to change the look and feel to any of the installed ones.

Image 1

Using the code  

Use the class below like this, where 'frame' is the JFrame you wish to modify the style of:

Java
public class Entry {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(new Dimension(200,200));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        new LookAndFeelPicker(frame);
    }
}

Copy the following class into your project and edit it as you wish:

Java
import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class LookAndFeelPicker extends JFrame 
{
    
    public LookAndFeelPicker(JFrame target)
    {
        setTitle("Style Picker");
        InitControls(target);
        pack();
        setVisible(true);
    }
    
    
    private void InitControls(JFrame target)
    {
        //Panels
        JPanel pnlContent = new JPanel();
        pnlContent.setLayout(new BorderLayout());
        getContentPane().add(pnlContent);
        
        //Labels
        JLabel lblInstructions = new JLabel("Please select a style:");
        pnlContent.add(lblInstructions, BorderLayout.PAGE_START);
        
        //List
        DefaultListModel dlmListData = new DefaultListModel();
        JList lstStyles = new JList(dlmListData);
        LookAndFeelInfo[] lafInfo = UIManager.getInstalledLookAndFeels();
        for(LookAndFeelInfo laf : lafInfo)
        {
            dlmListData.addElement(laf.getName());
        }
        lstStyles.addListSelectionListener(new StyleSelectionHandler(target));
        pnlContent.add(lstStyles, BorderLayout.CENTER);
    }
}

class StyleSelectionHandler implements ListSelectionListener
{
    private JFrame jfrFrameToUpdate;
    
    public StyleSelectionHandler(JFrame mainFrame)
    {
        jfrFrameToUpdate = mainFrame;
    }

    @Override
    public void valueChanged(ListSelectionEvent e) 
    { 
        JList lstStyles = (JList)e.getSource();
        if(!lstStyles.isSelectionEmpty())
        {
            try {
                UIManager.setLookAndFeel(
                  UIManager.getInstalledLookAndFeels()[lstStyles.getSelectedIndex()].getClassName());
                SwingUtilities.updateComponentTreeUI(jfrFrameToUpdate);
            } catch (ClassNotFoundException | InstantiationException
                    | IllegalAccessException | UnsupportedLookAndFeelException e1) {
                System.out.println(e1.getMessage());
            }
        }
    }
}

History 

Version 1 - Uploaded.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --