Click here to Skip to main content
13,252,362 members (59,620 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.8K views
1 bookmarked
Posted 20 Sep 2012

Java Swing Look and Feel Picker

, 20 Sep 2012
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

jhouns
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 20 Sep 2012
Article Copyright 2012 by jhouns
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid