How do I get the system look and feel?

The UIManager.getSystemLookAndFeelClassName() method return the current system look and feel for Swing application. Do not forget to call the SwingUtilities.updateComponentTreeUI() method after setting the LAF to update the current application look and feel.

package org.kodejava.example.swing;

import javax.swing.*;

public class SystemLAFDemo {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(250, 250);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setTitle("System LAF Demo");

        try {
            // Use the system look and feel for the swing application
            String className = UIManager.getSystemLookAndFeelClassName();
            System.out.println("ClassName = " + className);
            UIManager.setLookAndFeel(className);
        } catch (Exception e) {
            e.printStackTrace();
        }

        SwingUtilities.updateComponentTreeUI(frame);
        frame.setVisible(true);
    }
}

System LAF Demo

How do I set the look and feel for Swing application?

package org.kodejava.example.swing;

import javax.swing.*;
import java.awt.*;

public class LookAndFeelDemo extends JFrame {
    public LookAndFeelDemo() {
        initComponents();
    }

    private void initComponents() {
        setSize(250, 250);
        setTitle("LAF Demo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));

        JMenu menu = new JMenu("Look and Feel");

        // Get all the available look and feel that we are going to use for
        // creating the JMenuItem and assign the action listener to handle
        // the selection of menu item to change the look and feel.
        UIManager.LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels();
        for (UIManager.LookAndFeelInfo lookAndFeelInfo : lookAndFeels) {
            JMenuItem item = new JMenuItem(lookAndFeelInfo.getName());
            item.addActionListener(event -> {
                try {
                    // Set the look and feel for the frame and update the UI
                    // to use a new selected look and feel.
                    UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());
                    SwingUtilities.updateComponentTreeUI(this);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            menu.add(item);
        }

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu);

        getContentPane().add(menuBar);
        getContentPane().add(new JButton("Hello"));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new LookAndFeelDemo().setVisible(true));
    }
}
Look and Feel Demo

Look and Feel Demo

How do I get the available Swing look and feel?

The code snippet below describe how we can obtain the available look and feel in the current Swing platform. These information can then be made available to the user so they can change to look and feel of the application in their preferences.

package org.kodejava.example.swing;

import javax.swing.*;

public class AvailableLookAndFeel {
    public static void main(String[] args) {
        UIManager.LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels();

        for (UIManager.LookAndFeelInfo lookAndFeel : lookAndFeels) {
            // Get the name of the look and feel
            String name = lookAndFeel.getName();
            System.out.println("Name      = " + name);

            // Get the implementation class for the look and feel
            String className = lookAndFeel.getClassName();
            System.out.println("ClassName = " + className);
        }
    }
}

Below is the list of available look and feel produces by the code snippet above.

Name      = Metal
ClassName = javax.swing.plaf.metal.MetalLookAndFeel
Name      = Nimbus
ClassName = javax.swing.plaf.nimbus.NimbusLookAndFeel
Name      = CDE/Motif
ClassName = com.sun.java.swing.plaf.motif.MotifLookAndFeel
Name      = Mac OS X
ClassName = com.apple.laf.AquaLookAndFeel