Open In App

Java Swing - Look and Feel

Last Updated : 04 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Swing is a GUI Widget Toolkit for Java. It is an API for providing a Graphical User Interface to Java Programs. Unlike AWT, the Swing toolkit provides more advanced features such as animations and event handling.

Initially, there were very few options for colors and other settings in Java Swing, which made the entire application look boring and monotonous. With the growth in a Java framework, new changes were introduced to make the UI better and thus allow the developer to enhance the look of a Java Swing Application.

“Look” refers to the visual appearance of GUI widgets, and “feel” refers to the way the widgets behave. Sun’s JRE provides the following (L&F)s:

  • CrossPlatformLookAndFeel: this is the “Java L&F” also known as “Metal” that looks the same on all platforms. It is part of the Java API (javax.swing.plaf.metal) and is the default.
  • SystemLookAndFeel: here, the application uses the L&F that is default to the system it is running on. The System L&F is determined at runtime, where the application asks the system to return the name of the appropriate L&F. For Linux and Solaris, the System L&F's are “GTK+” if GTK+ 2.2 or later is installed, “Motif” otherwise. For Windows, the System L&F is “Windows”.
  • Synth: the basis for creating our look and feel with an XML file.
  • Multiplexing: a way to have the UI methods delegate to several different look and feel implementations at the same time.

We can use UIManager to load the L&F class directly from the classpath. For which the code goes like this:

UIManager.setLookAndFeel("fully qualified name of look and feel");

For example, the following code changes the application look and Feel to Motif Look And Feel:

UIManager.setLookAndFeel ("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

Check the available themes:

Java
// Java sample code to get the list of
// installed Look and Feel themes, here is a sample code:
import javax.swing.UIManager;

public class Geek
{    public static void main(String[] a)
    {
        UIManager.LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels();
        for (UIManager.LookAndFeelInfo look : looks) {
            System.out.println(look.getClassName());
        }
    }
}

Output:

UICheck


Now we will see different Look and feel themes with the help of a simple calculator program:

Example: Calculator program to use look and feel.

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

class Awt implements ActionListener {
    
    JFrame f;
    JButton addbut, subbut, mulbut, divbut, changeLFButton;
    JTextField t1, t2, t3;
    JLabel l;
    JComboBox<String> lookAndFeelComboBox;

    Awt() {
        
        f = new JFrame("Swing Calculator with Look and Feel");
        t1 = new JTextField(10);
        t2 = new JTextField(10);
        t3 = new JTextField(10);
        addbut = new JButton("Add");
        subbut = new JButton("Sub");
        mulbut = new JButton("Mul");
        divbut = new JButton("Div");
        
        changeLFButton = new JButton("Change Look and Feel");
        l = new JLabel("Result:");
        lookAndFeelComboBox = new JComboBox<>(new String[] {
            "Metal", "Nimbus", "Motif", "Windows", "System"
        });
    }

    public void awt1() {

        // Use GridBagLayout for better control
        f.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        // Add padding between components
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // Row 1: Enter Number 1
        gbc.gridx = 0;
        gbc.gridy = 0;
        f.add(new JLabel("Enter Number 1:"), gbc);

        gbc.gridx = 1;
        f.add(t1, gbc);

        // Row 2: Enter Number 2
        gbc.gridx = 0;
        gbc.gridy = 1;
        f.add(new JLabel("Enter Number 2:"), gbc);

        gbc.gridx = 1;
        f.add(t2, gbc);

        // Row 3: Result
        gbc.gridx = 0;
        gbc.gridy = 2;
        f.add(new JLabel("Result:"), gbc);

        gbc.gridx = 1;
        f.add(t3, gbc);

        // Row 4: Buttons (Add, Sub, Mul, Div)
        gbc.gridx = 0;
        gbc.gridy = 3;
        f.add(addbut, gbc);

        gbc.gridx = 1;
        f.add(subbut, gbc);

        gbc.gridx = 2;
        f.add(mulbut, gbc);

        gbc.gridx = 3;
        f.add(divbut, gbc);

        // Row 5: Look and Feel ComboBox and Button
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        f.add(lookAndFeelComboBox, gbc);

        gbc.gridx = 2;
        gbc.gridwidth = 2;
        f.add(changeLFButton, gbc);

        // Add action listeners
        addbut.addActionListener(this);
        subbut.addActionListener(this);
        mulbut.addActionListener(this);
        divbut.addActionListener(this);
        changeLFButton.addActionListener(this);

        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();
        if (s.equals("Change Look and Feel")) {
            String selectedLF = (String) lookAndFeelComboBox.getSelectedItem();
            try {
                switch (selectedLF) {
                    case "Metal":
                        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                        break;
                    case "Nimbus":
                        UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                        break;
                    case "Motif":
                        UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                        break;
                    case "Windows":
                        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                        break;
                    case "System":
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                        break;
                }
                SwingUtilities.updateComponentTreeUI(f);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(f, "Error setting Look and Feel: " + ex.getMessage());
            }
        }
        else {
            try {
                int a = Integer.parseInt(t1.getText().trim());
                int b = Integer.parseInt(t2.getText().trim());
                int result = 0;

                if (s.equals("Add")) {
                    result = a + b;
                } else if (s.equals("Sub")) {
                    result = a - b;
                } else if (s.equals("Mul")) {
                    result = a * b;
                } else if (s.equals("Div")) {
                    if (b == 0) {
                        t3.setText("Error: Division by 0");
                        return;
                    }
                    result = a / b;
                }

                t3.setText(String.valueOf(result));
            } catch (NumberFormatException ex) {
                t3.setText("Invalid Input");
            }
        }
    }

    public static void main(String args[]) {

        
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println("Look and Feel not set: " + e.getMessage());
        }

        Awt a = new Awt();
        a.awt1();
    }
}

Output:


Explanation: In the above code, we use the different Look and Feel themes to enhance the User Interface. We can also use different themes there are options to download different themes.

Output In Different Themes

These are the images which show how the UI looks different in various themes.

1. Using NImbus Theme

UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");

Nimbus


2. Using MotifLookAndFeel Theme

UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

motif


3. Using Metal Theme

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

Metal


4. Using Windows Look and Feel Theme

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

Windows


5. Using System Theme (Default)

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

System


Specifying Look And Feel using Command Line Arguments

We can specify the Look And Feel by using the -D flag at the command line to set the swing. defaultlaf property.

java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel Awt

There is an option for professional themes which one can download and use in the code. Here is the list of professional themes that are available:

  • Substance
  • Sea Glass
  • Info Look And Feel
  • Pgs. Look And Feel
  • Quaqua Look And Feel
  • Oyaha
  • Liquid Look And Feel
  • JTattoo

Article Tags :
Practice Tags :

Similar Reads