Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Your Email:

Advanced Synth

By Michael Abernethy
2005-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();

synth.load(SynthFrame.class.getResourceAsStream("demo.xml"), SynthFrame.class);
UIManager.setLookAndFeel(synth);
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.

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">

// 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"/>
One note about the <bind> tag: The key attribute inside the tag maps to constants in the javax.swing.plaf.synth.Region class. The Synth engine uses these constants to link the style with an actual Swing component. Simple components, such as JButton and JTextField, use one constant. Some more-complex components, such as the JScrollBar and the JTabbedPane, have multiple constants for their different parts.

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


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT