Java Swing | Look and Feel

Swing is GUI Widget Toolkit for Java. It is an API for providing Graphical User Interface to Java Programs. Unlike AWT, Swing components are written in Java and therefore are platform-independent. Swing provides platform specific Look and Feel and also an option for pluggable Look and Feel, allowing application to have Look and Feel independent of underlying platform.
Initially there were very few options for colors and other settings in Java Swing, that made the entire application look boring and monotonous. With the growth in Java framework, new changes were introduced to make the UI better and thus giving developer opportunity to enhance the look of a Java Swing Application.

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

  1. 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.
  2. 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&Fs are “GTK+” if GTK+ 2.2 or later is installed, “Motif” otherwise. For Windows, the System L&F is “Windows”.
  3. Synth: the basis for creating your own look and feel with an XML file.
  4. Multiplexing: a way to have the UI methods delegate to a number of different look and feel implementations at the same time.

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



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

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

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

edit
close

play_arrow

link
brightness_4
code

// Java sample code to get the list of
// installed Look and Feel themes, here is a sample code:
import javax.swing.UIManager;
  
public class MainClass {    public static void main(String[] a)
    {
        UIManager.LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels();
        for (UIManager.LookAndFeelInfo look : looks) {
            System.out.println(look.getClassName());
        }
    }
}

chevron_right


GfGinstalledL&F

We will see different Look and Feel themes with the help of a simple calculator program:

  1. CrossPlatformLookAndFeel:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // CrossPlatformLookAndFeel
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.UIManager;
    import javax.swing.JFrame;
    class Awt implements ActionListener {
        JFrame f;
        JButton addbut, subbut, mulbut, divbut, b5;
        JTextField t1, t2, t3;
        JLabel l, l1;
        Awt()
        {
            f = new JFrame("Cross Platform Look and Feel");
            t1 = new JTextField("             ");
            t2 = new JTextField("             ");
            t3 = new JTextField("             ");
            addbut = new JButton("Add");
            subbut = new JButton("Sub");
            mulbut = new JButton("Mul");
            divbut = new JButton("Div");
            l = new JLabel();
            l1 = new JLabel();
        }
        public void awt1()
        {
            f.setLayout(new GridLayout(3, 2));
            f.setVisible(true);
            f.add(t1);
            f.add(t2);
            f.add(t3);
            f.add(addbut);
            f.add(subbut);
            f.add(mulbut);
            f.add(divbut);
            f.add(l);
            f.add(l1);
            addbut.addActionListener(this);
            subbut.addActionListener(this);
            mulbut.addActionListener(this);
            divbut.addActionListener(this);
            f.pack();
        }
        public void actionPerformed(ActionEvent e)
        {
            String s = new String(e.getActionCommand());
            l.setText(s);
            if ((s).equals("Add")) {
                int a = Integer.parseInt(t1.getText());
                int b = Integer.parseInt(t2.getText());
                Integer c = a + b;
                t3.setText(c.toString());
            }
            else if ((s).equals("Sub")) {
                int a = Integer.parseInt(t1.getText());
                int b = Integer.parseInt(t2.getText());
                Integer c = a - b;
                t3.setText(c.toString());
            }
            else if ((s).equals("Mul")) {
                int a = Integer.parseInt(t1.getText());
                int b = Integer.parseInt(t2.getText());
                Integer c = a * b;
                t3.setText(c.toString());
            }
        }
      
        public static void main(String args[])
        {
      
            try {
      
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            }
            catch (Exception e) {
                System.out.println("Look and Feel not set");
            }
            Awt a = new Awt();
            a.awt1();
        }
    }

    chevron_right

    
    

    GfGCrossPlatform
    You can also use actual class name of Look And Feel as argument to UIManager.setLookAndFeel(). For example,
    // Set cross-platform Java L&F (also called “Metal”)

    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  2. MotifLookAndFeel: Just change Line number 14 and 79 to:
     Line 14: f=new JFrame("Motif Look and Feel");
    Line 79: UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

    GfGMotif

  3. SystemLookAndFeel: Change Line number 14 and 79 to:
    Line 14: f=new JFrame("System Look and Feel");
    Line 79: UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

    GfGSystem

  4. WindowsClassicLookAndFeel: Change Line number 14 and 79 to:
    Line 14: f=new JFrame("WindowsClassic Look and Feel");
    Line 79: UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

    GfGWindowsClassic

  5. NimbusLookAndFeel: Change Line number 14 and 79 to:
    Line 14: f=new JFrame("Nimbus Look and Feel");
    Line 79: UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");

    GfGNimbus

Specifying Look And Feel using Command Line Arguments

You can specify the Look And Feel by using -D flag at the command line to set the swing.defaultlaf property. Example –
We will run the above code excluding Line 76 and executing following command:


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

GfGCommandLine

Specifying Look And Feel by editing swing.properties file

In this we will edit swing.properties file to set the swing.defaultlaf property. This file is located in lib directory. Here is an example-

swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel

Themes

Themes were introduced to change colors and fonts of default Java Metal Look And Feel. A constant can be initialised to one of 3 values:

  • Default Metal
  • Ocean
  • Test

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

  1. Substance
  2. Sea Glass
  3. Info Look And Feel
  4. Pgs Look And Feel
  5. Quaqua Look And Feel
  6. Oyaha
  7. Liquid Look And Feel
  8. JTattoo

References:

  1. https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  2. http://geeknizer.com/best-java-swing-look-and-feel-themes-professional-casual-top-10/


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.