How to use Windows XP theme in Java/Swing applications?

10,717

Solution 1

I usually use this line

javax.swing.UIManager.setLookAndFeel( javax.swing.UIManager.getSystemLookAndFeelClassName());

because if you run the application in another system you could haven't windows look and feel.

or you can use this:

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("<lookAndFeelName>".equals(info.getName())) {
        javax.swing.UIManager.setLookAndFeel(info.getClassName());
        break;
    }
}

because in this way, if you want to force the windows Look and feel you should verify if this lookAndFeel exists

Or you have 2 good ways to set lookAndFeel without check if exists:

try {
    javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    //javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
} catch (ClassNotFoundException ex) {
    //Handle Exception
} catch (InstantiationException ex) {
    //Handle Exception
} catch (IllegalAccessException ex) {
    //Handle Exception
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
    //Handle Exception
}

or simply:

try {
    javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    //javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
} catch (Exception ex) {
    //Handle Exception
}

The difference between WindowsLookAndFeel and WindowsClassicLookAndFeel, is that "WindowsLookAndFeel" will corresponds to user Windows Theme and "WindowsClassicLookAndFeel" will force java application GUI to match Windows Classic Theme even if user has not set their Windows Theme to classic.

So you can force classic but you can't force Vista/7 LookAndFeel using Windows Classic Theme.

Or you can design your application to anything like Vista/7 LookAndFeel (just kidding)

Solution 2

One way is to implement it in constructor:

public class LookS extends JFrame { 

  String win1 = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
  String win2 = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";

public LookS(){
...

        try {
            UIManager.setLookAndFeel(win1);
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.setDefaultLookAndFeelDecorated(true);
...
}

Also, you can consider these classes too:

  String srt1 = "javax.swing.plaf.metal.MetalLookAndFeel";
  String srt2 = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
  String srt3 = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
  String srt4 = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
Share:
10,717
Sudhir
Author by

Sudhir

A Java/J2EE developer

Updated on August 21, 2022

Comments

  • Sudhir
    Sudhir almost 2 years

    I have to use Windows XP theme in my Java/Swing project. There are classes like below.

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

    WindowsLookAndFeel can be used when someone wants to use the same theme as currently applied by the user.

    com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel
    

    WindowsClassicLookAndFeel can be used when someone wants to specifically use the windows classic theme.

    When we change a theme of Windows from XP to classic, swing UI also changes from XP to classic. How can we force it using aforementioned classes?