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

package org.kodejava.swing;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import java.awt.FlowLayout;

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

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

    private void initComponents() {
        setSize(500, 500);
        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"));
    }
}
Java Swing Look and Feel (LAF)

Java Swing Look and Feel (LAF)

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.