Share this page 

Use any LookAndFeel on any plateformTag(s): Swing


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();
}