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 | 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
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 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 | JWindow with examples JWindow is a part of Java Swing and it can appear on any part of the users desktop. It is different from JFrame in the respect that JWindow does not have a title bar or window management buttons like minimize, maximize, and close, which JFrame has. JWindow can contain several components such as butt
5 min read
Java Swing | Internal Frame with examples JInternalFrame is a part of Java Swing . JInternalFrame is a container that provides many features of a frame which includes displaying title, opening, closing, resizing, support for menu bar, etc. Constructors for JInternalFrame JInternalFrame() : creates a new non- closable, non- resizable, non- i
8 min read