How to change the theme of a java GUI Application (Java look and feel)

Hi, today I’m going to show you how to change the appearance of a Java GUI application. In technically we call it as changing the look and feel of a Java GUI application. Swing components are responsible for changing the look and feel. In here “Look” refers to appearance of the GUI component and “Feel” refers to behave of the GUI component.

Java

Sun’s (now it’s Oracle) default JDK has provided these basic  LookAndFeel types..

  1. javax.swing.plaf.metal.MetalLookAndFeel
  2. com.sun.java.swing.plaf.windows.WindowsLookAndFeel
  3. com.sun.java.swing.plaf.motif.MotifLookAndFeel
  4. com.sun.java.swing.plaf.gtk.GTKLookAndFeel

When we developing  an GUI application using Java , it normally gives normal look and feel smiler to Linux OS (especially Knoppix-Linux  look and feel). Because by default it sets to Sun’s CrossPlatformLookAndFeel and also called “Metal look and feel“.And it’s part of the Java API (javax.swing.plaf.metal).

This code should be included in main method..

try {
                    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());                

                    } catch (Exception e) {
                      System.err.println(“Look and feel not set.”);
                    }

for example..

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class gui {

    public static void main(String args[]){

                //———–Look and Feel————-
                try {
                    UIManager.setLookAndFeel(“com.sun.java.swing.plaf.motif.MotifLookAndFeel”);             

                    } catch (Exception e) {
                      System.err.println(“Look and feel not set.”);
                    }
                //———–Look and Feel————-
         gui myGui = new gui();
            myGui.launchFrame();
    }//end main()

But if you choose SystemLookAndFeel the application gives a look and feel like the running Operating system. Simply I say if you cshoose this, the look and feel will be depended on the Operating system.. To do that add following codes into your GUI application’s main method of the main class.

try {
                      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

                    } catch (Exception e) {
                      System.err.println(“Look and feel not set.”);
                    }
But if you choose WindowsLookAndFeel, the application gives a look and feel like the Windows Operating system.(But this will work only on Windows)

try {
                     UIManager.setLookAndFeel(“com.sun.java.swing.plaf.windows.WindowsLookAndFeel”);

                    } catch (Exception e) {
                      System.err.println(“Look and feel not set.”);
                    }

If you want to get Sun’s Motif Look and feel use this….

try {
                      UIManager.setLookAndFeel(“com.sun.java.swing.plaf.motif.MotifLookAndFeel”);

                    } catch (Exception e) {
                      System.err.println(“Look and feel not set.”);
                    } 

But in addition to that you may need an advanced look and feel.. for that you can use any commercial look and feels like Synthetica look and feel or Jtatoo.

with Syanthatica look and feel it look like this…

import de.javasoft.plaf.synthetica.SyntheticaBlackEyeLookAndFeel;

try {
UIManager.setLookAndFeel(new SyntheticaBlackEyeLookAndFeel());

} catch (Exception e) {
System.err.println(“Look and feel not set.”);
}

here I used sysnthatica’s Black eye look and feel. Other than the default look feela of Java, here you need to download and set the build path for some .jar file they provide.

To do that first you need to download Syanthatica’s synthatica.jar from this URL :http://www.jyloo.com/synthetica/download/

then, you need to download a theme for that :http://www.jyloo.com/synthetica/themes/

If you are using  Netbeans for java application developing put that both two .jar file into Libraries folder. If you are using Eclipse, put that two .jar files into JRE system libraries by right clicking and selecting “build path”–> “configure build path”.

Then Jtatoo is also a good commercial look and feel set. If you want to to try that, you need to download Jtatoo look and feel at:http://www.jtattoo.net/Download.html

There will be a file call Jtatoo.jar , so as mentioned above you need to put that into appropriate folder, then you can use it like this..

try {
                     UIManager.setLookAndFeel(“com.jtattoo.plaf.smart.SmartLookAndFeel”);

                    } catch (Exception e) {
                      System.err.println(“Look and feel not set.”);
                    }

In addition if you need an GUI application to test this you can use mine.

My java file:gui.java

My Eclipse project:My Code.zip

If you have any doubt, feel free to comment them.

Thanks

Gihan Malan De Silva


9 Comments

  1. Reply
    Thilanka Bodhikotuwa June 19, 2011

    Good explanation nicely done.

  2. Reply
    Asfer Mohamed June 19, 2011

    Hi Gihan. If you can , add post list in sidebar. bcz it is difficult to find previous post

    • Reply
      Gihan June 19, 2011

      Thanks Asfer, I’ll fix it.

  3. Reply
    java2012 June 27, 2011

    Niyamai machoooo……..

  4. Reply
    Leyth November 17, 2011

    Very good explanation, i did like u wrote, but its different in Netbeans, u have to use only one of these lines into Look and Feel Settings.

    like this instead all the other lines. UIManager.setLookAndFeel(“com.sun.java.swing.plaf.motif.MotifLookAndFeel”);

    but i couldnt make it without ur site, thx again!

  5. Reply
    nitin harane April 11, 2012

    very good and clear explanation but i hv dought how to set path for jar files while deploying project

  6. Reply
    kasun September 2, 2012

    maxa macho

  7. Reply
    Arjun Mota October 9, 2012

    Thank you,
    if any new code found then please post it on this.

  8. Reply
    vighanesh October 25, 2012

    Hey bro i’m trying to put synthetica look and feel in my app. But for some reason it is showing me an exception that you must provide style factory .

Leave a Reply