« September 2007 | Main | November 2007 »

October 2007 Archives

October 17, 2007

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 {
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    MetalLookAndFeel.setCurrentTheme(new BurnMyEyesOutBlueOceanTheme())
} catch (ClassNotFoundException cnfe) {
} catch (InstantiationException ie) {
} catch (IllegalAccessException iae) {
} catch (UnsupportedLookAndFeelException 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('plasticXP', tabStyle:'metal')

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 October 2007

This page contains all entries posted to ... And They Shall Know Me By My Speling Errors in October 2007. They are listed from oldest to newest.

September 2007 is the previous archive.

November 2007 is the next archive.

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

Powered by
Movable Type 3.33