Ask Questions?

View Latest Questions

Advertisement


 
 

Look and Feel
Posted on: July 26, 2006 at 12:00 AM
How to use the default Java cross-platform Look & Feel.

Java Notes

Look and Feel

For best portability use the default Java cross-platform Look & Feel. There are several good reasons to change it however.

  • Avoid ugliness. Before Java 5, the default L&F, Metal, was relatively unattractive.
  • System L&F. It's nice to match the system L&F, eg, on Windows the program should have a Windows L&F, on the Macintosh it should use the standard Macintosh L&F..
  • Special. You would like some special appearance.

The javax.swing.UIManager class methods can be used to set it.

To Set the Look and Feel

try {
    UIManager.setLookAndFeel(XXX);
} catch (Exception e) {
    // This is one of the few places where silently ignoring an exception
    // might be the right thing to do, altho it is almost always
    // a very bad practice.  What else can we do in this case?
    // The default look & feel will be used if we ignore this.
}

Where XXX is one of the following:

The Java cross-platform L&F
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
The current OS L&F
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Windows L&F
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel" can currently only be used on Windows.
Macintosh L&F
"javax.swing.plaf.mac.MacLookAndFeel" can currently only be used on the Macintosh [still true?]. Note: The look and feel code doesn't move menu elements around to match the the relevant system.
CDE/Motif Look and Feel
"com.sun.java.swing.plaf.motif.MotifLookAndFeel" can be used on any system.

Custom Look and Feel

A number of custom Looks and Feels have been developed. Take a look at www.javootoo.com for examples and links to some. A nice L&F for informal appearance is Napkin, at napkinlaf.sourceforge.net/.

You can easily install these: just put the .jar file in the CLASSPATH, and make the appropriate call as above. For example, to use the Kunststoff L&F, add the .jar file to your classpath and make the following call.

UIManager.setLookAndFeel(new com.incors.plaf.kunststoff.KunststoffLookAndFeel());

Dynamically updated Look & Feel

Most programs don't change the L&F, and those that do will set the L&F at the beginning, before any GUI elments have been created. If you want to dynamically change the L&F, you must call updateComponentTreeUI(), passing it the component, typically your window, that must be updated to the new Look & Feel.

UIManager.setLookAndFeel(...);
SwingUtilities.updateComponentTreeUI(myWindow);

Java 5 skinnable Synth Look&Feel

Java 5 introduced the Synth L&F, which can be customized by changing an XML text file, making it relatively easy to customize a L&F. Take a look at the following.

Copyleft 2004 Fred Swartz MIT License
Advertisement


DMCA.com