Skip to main content

Pass Parameters to Painters of Synth Look And Feel

Posted by xuanyun on January 30, 2009 at 2:17 AM PST

Synth look and feel allow us to implement our own painter classes to render
the components, that is very good since we can draw anything in our painter
class. But painter class is still a Java class, we need to compile it every
time once we need to change a little details in the class, so it is better to
design a painter class that can accept parameters, which can be easily changed
in the XML.

Basically there are two ways to do so: using defaultsProperty
or property tag in the XML:

Using defaultsProperty:

The parameters defined in the XML can be retrieved via UIManager,
here is an example from EaSynth
look and feel

In easynth.xml file we defined two parameters, one integer
and one Color object.

   <style id="Progress Bar">
   <defaultsProperty key="EaSynth.progressbar.arc.width" type="integer" value="10"/>
   <object id="Progressbar_Background_Color" class="java.awt.Color">
   <defaultsProperty key="EaSynth.progressbar.background.color" type="idref" value="Progressbar_Background_Color"/>

In the EaSynthPainter class we can retrieve them.

   public void paintProgressBarBackground(SynthContext context, Graphics g, int x, int y, int w, int h) {
   final UIDefaults uiDefaults = UIManager.getDefaults();
   final Color bgColor = uiDefaults.getColor("EaSynth.progressbar.background.color");
   if (bgColor != null) {
   final int arcWidth = uiDefaults.getInt("EaSynth.progressbar.arc.width");

Using property:

We can define the parameter with property tag in the XML, it can be retrieved
via SynthContext. Example from EaSynth
look and feel

   <imageIcon id="Progressbar_Indication_Image" path="resource/1204213052578_progressbar_indication.png"/>
   <property key="EaSynth.progressbar.indication.image" type="idref" value="Progressbar_Indication_Image"/>

The following code in EaSynthPainter can retrieve them.

   public void paintProgressBarBackground(SynthContext context, Graphics g, int x, int y, int w, int h) {
   final ImageIcon bgIcon = (ImageIcon) context.getStyle().getIcon(context, "EaSynth.progressbar.background.image");

Both two approaches work well for passing parameters to Synth painter. But
if you want to pass parameters to any objects
embedded in Synth
look and feel, you should use the first approach, since
the SynthContext may not be accessible in other classes.

Related Topics >>


Hi magnum, I am happy that you like it. I will share more experiences about Synth L&F development in future posts.

Your posts are really very interesting. It's not easy to find resources on the Web about synth Look & Feel developments. I had a hard time at the beginning to find what I needed when I wanted to use synth in one of my projects ;) But synth is ultimately very powerful.