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

18 06 2011

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



Actions

Information

9 responses

19 06 2011
Thilanka Bodhikotuwa

Good explanation nicely done.

19 06 2011
Asfer Mohamed

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

19 06 2011
Gihan

Thanks Asfer, I’ll fix it.

27 06 2011
java2012

Niyamai machoooo……..

17 11 2011
Leyth

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!

11 04 2012
nitin harane

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

2 09 2012
kasun

maxa macho

9 10 2012
Arjun Mota

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

25 10 2012
vighanesh

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: