Java Swing - Look and Feel
Last Updated :
04 Apr, 2025
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:
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");
2. Using MotifLookAndFeel Theme
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
3. Using Metal Theme
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
4. Using Windows Look and Feel Theme
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
5. Using System Theme (Default)
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
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
Similar Reads
Java Swing | Look and Feel
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, whic
5 min read
Java Swing | BevelBorder and SoftBevelBorder
BevelBorder and SoftBevelBorder are a part of javax.swing.Border package. This package contains different Border for Components. BevelBorder is an implementation of a simple two line bevel border. Bevel Border and Soft Bevel Border are almost same but Soft Bevel Border has softened corners.Construct
7 min read
Java Swing | JToolBar
JToolBar is a part of Java Swing package. JToolBar is an implementation of toolbar. The JToolBar is a group of commonly used components such as buttons or drop down menu. JToolBar can be dragged to different locations by the user Constructors of the class are: JToolBar() : creates a new toolbar with
6 min read
JLabel | Java Swing
JLabel is a class of java Swing . JLabel is used to display a short string or an image icon. JLabel can display text, image or both . JLabel is only a display of text or image and it cannot get focus . JLabel is inactive to input events such a mouse focus or keyboard focus. By default labels are ver
4 min read
Java Swing | JDialog with examples
JDialog is a part Java swing package. The main purpose of the dialog is to add components to it. JDialog can be customized according to user need .Constructor of the class are: JDialog() : creates an empty dialog without any title or any specified ownerJDialog(Frame o) :creates an empty dialog with
5 min read
Java Swing | JSlider
JSlider is a part of Java Swing package . JSlider is an implementation of slider. The Component allows the user to select a value by sliding the knob within the bounded value . The slider can show Major Tick marks and also the minor tick marks between two major tick marks, The knob can be positioned
6 min read
Difference between AWT and Swing in Java
Java is one of the most in-demand programming languages for developing a variety of applications. The popularity of Java can be attributed to its versatility as it can be used to design customized applications that are light and fast and serve a variety of purposes ranging from web services to andro
2 min read
Java Swing | JList with examples
JList is part of Java Swing package . JList is a component that displays a set of Objects and allows the user to select one or more items . JList inherits JComponent class. JList is a easy way to display an array of Vectors .Constructor for JList are : JList(): creates an empty blank listJList(E [ ]
4 min read
Java AWT Toolkit
The Abstract Window Toolkit (AWT) is a Java package that provides a platform-indepеndеnt sеt of tools for creating graphical usеr intеrfacеs (GUIs). AWT is part of thе Java Foundation Classеs (JFC), which also includes Swing for morе advancеd GUI dеvеlopmеnt. In this rеsponsе, I'll providе an ovеrvi
6 min read
Java Swing - JPanel With Examples
JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar. Constructors of JPanel JPanel
4 min read