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