Jump to Real's How-to Main page

Use any LookAndFeel on any plateform

There is some restriction to the usage of LookAndFeel when there are some copyright involved. For example, you can't activate the Mac LookAndFeel in a Windows environment.

The trick is to fool Swing by setting the property os.name to a different value than real one to enable the use of "forbidden" LookAndFeel.

From the command line,
to activate Windows LookAndFeel on a non-Windows environment

java -Dos.name=windows MySwingApp
to activate Mac LookAndFeel on a non-Mac environment
java -Dos.name=mac MySwingApp
or you can do it in your source by doing something like:
Properties p = System.getProperties();
p.put("os.name", "Mac");
System.setProperties(p);
NOTE: Current Swing release for Windows does not include the necessary classes for the Mac's LookAndFeel anymore.

It's not bad idea to set the look and feel to a known good value and then try the not-so-sure value.

try {
 // sure look and feel
 UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
 
 // not-so-sure look and feel
 System.setProperty("os.name", "Windows");
 System.setProperty("os.version", "5.1");
 UIManager.setLookAndFeel(
   "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
 } 
catch (Exception ex) {
 ex.printStackTrace();
}

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]