Advanced Synth
By Michael Abernethy2005-04-16
Synth Basics
Synth is a tabula rasa look -- a completely blank canvas that appears as an all-white panel until some components are defined in the XML file. Once you define the components, setting the Synth look and feel on an application couldn't be easier, as you can see in Listing 1:
Listing 1. Setting the Synth look and feel
SynthLookAndFeel synth = new SynthLookAndFeel();But with Synth it's the XML code, not the Java code, that's most important to understand. Although the Synth XML format initially seems difficult and intimidating, it's really rather straightforward. If you use the KISS (Keep It Simple Stupid) mantra, you can quickly create an XML file and get a new look and feel up and running.
synth.load(SynthFrame.class.getResourceAsStream("demo.xml"), SynthFrame.class);
UIManager.setLookAndFeel(synth);
With the KISS directive in mind, I'll start by introducing the main building block of the Synth XML file -- the <style> tag. The <style> tag contains all the information needed to describe a component's style, such as the colors, fonts, image files, states, and component-specific properties. Although a single <style> tag can describe more than one component, the easiest way to build a Synth file is to create a style for each individual Swing component.
Once you finish creating the style, you link the style to a component. The <bind> tag tells the Synth engine to link a defined style to a component, as in Listing 2. This combination completely creates the component's new look.
Listing 2. Linking one style to one component
<style id="textfield">One note about the <bind> tag: The key attribute inside the
// describe colors, fonts, and states
</style>
<bind style="textfield" type="region" key="Textfield"/>
<style id="button">
// describe colors, fonts, and states
</style>
<bind style="button" type="region" key="Button"/>
I recommend using the one-style-per-component setup until you are more familiar with the Synth format and can set up inheritance models in the XML. This structure doesn't take advantage of all of XML's hierarchical capabilities, but it's the most straightforward to set up, code, and debug.
Another important point to remember when dealing with the Synth XML file is that it doesn't validate anything. If you make typographical errors or use incorrect attributes in the XML, your mistake won't show up until a runtime exception is thrown when the look and feel is loaded. Translation: Test the XML file before you ship it to a customer.
Tutorial Pages:
» Custom UIs are a Breeze with the Newest Swing Look and Feel
» Beauty's Only Skin Deep
» Synth Basics
» Demo Application
» Changing a Color and Font
» Using Images
» Handling Different States
» Working with Custom Painters
» More-Advanced Settings
» Examining Synth Performance, Reliability, and Efficiency
» Conclusion
» Resources
First published by IBM DeveloperWorks
