« New and Mildly Entertaining features in SwingBuidler | Main | MVC Separation in SwingBuilder Using the build() method »

SwingBuilder + LookAndFeel = Prettier Setup Code

Groovy 1.1 is quickly getting ready to ship, but since no one has slapped my had yet I am still adding new features! That is if I'm not taking them away: the animate() node has been moved from SwingBuilder to SwingXBuilder because of it's dependency on the SwingLabs TimingFramework (good stuff Chet, once I started I couldn't stop).

Why add look and feel support? It is relatively easy.. if a tad bit verbose.

try {
    UIMangaer.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    MetalLookAndFeel.setCurrentTheme(new BurnMyEyesOutBlueOceanTheme())
} catch (ClassNotFoundException cnfe) {
    logger.error(cnfe);
} catch (InstantiationException ie) {
    logger.error(ie);
} catch (IllegalAccessException iae) {
    logger.error(iae);
} catch (UnsupportedLookAndFeelException ulafe) {
    logger.error(ulafe);
}

The exception block could be wrapped into a single 'catch (Exception e)' (if your static analysis tool doesn't shout you down first). or we could init the look and feel directly via 'new MetalLookAndFeel() and get rid of three of the four exceptions. But the boldMetal change is quite a silly incantation, and there is still alot of typing for your IDE to do when you press ctrl-space. How does the groovy code look?

swing.lookAndFeel('metal', boldFonts:false, 
    theme: new BurnMyEyesOutBlueOceanTheme())

Much simpler. And I didn't go through all this trouble just for bold fonts either...

swing.lookAndFeel('substance',
    watermark:'org.jvnet.substance.watermark.SubstanceNoneWatermark',
    skin:'org.jvnet.substance.skin.BusinessBlueSteelSkin',
    theme:'org.jvnet.substance.theme.SubstanceBrownTheme')
swing.lookAndFeel('plasticXP', tabStyle:'metal')
swing.lookAndFeel('win2k')

Any attribute that is settable on the look and feel instance or it's static class can be set via the attributes. There are also some special case attributes, namely boldFonts: in metal, noxp: in windows, and some muti-dispatch handling for some substance properties. I also have class names loaded in for 'nimbus', 'aqua', 'gtk', and 'synth' among a few others. It also supports a closure where the look and feel is passed in as the parameter, in case there is any really bizzare setup to do.

One caveat though: don't call this inside the builder. The reason is that the nodes that are created before the lookAndFeel() call will not have their UIDefaults set properly. So call it before all other calls and do it as it's own top level node.

About

This page contains a single entry from the blog posted on October 17, 2007 4:15 PM.

The previous post in this blog was New and Mildly Entertaining features in SwingBuidler.

The next post in this blog is MVC Separation in SwingBuilder Using the build() method.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33