Click here to Skip to main content
Click here to Skip to main content

Java Swing Look and Feel Picker

By , 20 Sep 2012
 

Introduction

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

Using the code  

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

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:

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)

About the Author

jhouns
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 20 Sep 2012
Article Copyright 2012 by jhouns
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid