Xavier has posted 3 posts at DZone. View Full User Profile

EaSynth Look And Feel Customization (1)

08.15.2008
| 8572 views |
  • submit to reddit

EaSynth look and feel is an extended Synth look and feel from www.easynth.com, it is free and open-source under Apache license 2.0, we can use it in our Java application for free. But the default theme of EaSynth look and feel may not meet our requirements all the time, sometimes we may need to make some customization on EaSynth look and feel to obtain more suitable GUI style.

As we already know, the Synth look and feel does not provide a "default" theme, if we create an instance of javax.swing.plaf.synth.SynthLookAndFeel class (without loading any XML configuration file) and set it as the current look and feel in the UIManager, you will see an "empty" theme (shown in the following figure), which is not acceptable at all.

The empty theme of Synth look and feel

So before we use Synth look and feel in our products, we have to make proper configuration for all GUI component styles in an XML file and load it into the look and feel object. There are 50+ GUI components defined in swing, so creating a brand new look and feel from scratch is a little complex, but we can use EaSynth look and feel as a template and make customization on it, which also able to provide a brand new look and feel with less effort.

I will introduce the customization for different GUI components on EaSynth look and feel in a serial of articles, this article is the first one, which will introduce the customization on regular button in EaSynth look and feel.

We can customize the Synth based look and feel by changing its XML configuration file, painter class file and graphics utility class file. What's more, EaSynth look and feel provides some open properties, which allow us to custom the behavior of its painter and graphics utility.

EaSynth look and feel is built with a commercial IDE product "EaSynth Look And Feel Designer", the project file "EaSynthLookAndFeel.lfp" is also enclosed in the open source bundle. If you have the license, you can use that IDE to perform the customization, which will be a much easier job. But in these articles, I will focus on the way that performing customization without IDE tool.

Regular Button Customization

The following figure shows the four basic states of the regular button:

The button style for different states

First we can change the background images of the button, they are specified in "easynth.xml" file, which is enclosed in the "com/easynth/lookandfeel" folder inside "EaSynthLookAndFeel.jar" file. Now let's open the "easynth.xml" file and locate the "style" node with id "Button", we will see the following:

<state value="ENABLED">
    <imagePainter method="buttonBackground" path="resource/1204727000093_button_enabled.png" sourceInsets="5 5 5 5" paintCenter="true" stretch="true" center="false"/>
</state>
<state value="MOUSE_OVER">
    <imagePainter method="buttonBackground" path="resource/1204727022828_button_mouseover.png" sourceInsets="5 5 5 5" paintCenter="true" stretch="true" center="false"/>
</state>
<state value="PRESSED">
    <imagePainter method="buttonBackground" path="resource/1204727039640_button_pressed.png" sourceInsets="5 5 5 5" paintCenter="true" stretch="true" center="false"/>
</state>
<state value="DISABLED">
    <imagePainter method="buttonBackground" path="resource/1204727048156_button_disabled.png" sourceInsets="5 5 5 5" paintCenter="true" stretch="true" center="false"/>
</state>

For each state, an imagePainter element is defined, the "method" attribute is set to "buttonBackground", means the imagePainter element is used to specify the background image for regular button. The "path" attribute present the location of the background image file. So the customization will be easy, we can just put some new images file into the "com\easynth\lookandfeel\resource" folder inside "EaSynthLookAndFeel.jar" file and then change the path attributes for imagePainter elements in the XML.

Here are my new background images for regular button:

New background images

I put them into the JAR file and then modify the XML as follow:

<state value="ENABLED">
    <imagePainter method="buttonBackground" path="resource/new_button_enabled.png" sourceInsets="5 5 5 5" paintCenter="true" stretch="true" center="false"/>
</state>
<state value="MOUSE_OVER">
    <imagePainter method="buttonBackground" path="resource/new_button_mouseover.png" sourceInsets="5 5 5 5" paintCenter="true" stretch="true" center="false"/>
</state>
<state value="PRESSED">
    <imagePainter method="buttonBackground" path="resource/new_button_pressed.png" sourceInsets="5 5 5 5" paintCenter="true" stretch="true" center="false"/>
</state>
<state value="DISABLED">
    <imagePainter method="buttonBackground" path="resource/new_button_disabled.png" sourceInsets="5 5 5 5" paintCenter="true" stretch="true" center="false"/>
</state>

After update this XML to the JAR file, and applay the new look and feel JAR file in an example Java project, we can see the effect:

New background effect

You will notice that the border of the button is not changed yet, but sometimes we need to change it as well. There are two open properties defined in EaSynth look and feel to define the dimension of the arc used in the border round corner:

  • EaSynth.button.arc.width: the width of the ellipse that the arc comes from.
  • EaSynth.button.arc.height: the height of the ellipse that the arc comes from.

The figure below shows that details for these two open properties:

 The arc in the border of button

We can change the values of these properties in the XML, but keep in mind that the round corner of border should be consist with that of the background image:

<defaultsProperty key="EaSynth.button.arc.width" type="integer" value="9"/>
<defaultsProperty key="EaSynth.button.arc.height" type="integer" value="9"/>

We can also specify the border color for all states, there are another set of open properties defined to custom the border color, listed below:

<defaultsProperty key="EaSynth.button.border.color.enabled" type="idref" value="Button_Border_Color_Enabled"/>
<defaultsProperty key="EaSynth.button.border.color.mouseover" type="idref" value="Button_Border_Color_MouseOver"/>
<defaultsProperty key="EaSynth.button.border.color.pressed" type="idref" value="Button_Border_Color_Pressed"/>
<defaultsProperty key="EaSynth.button.border.color.disabled" type="idref" value="Button_Border_Color_Disabled"/>
<defaultsProperty key="EaSynth.button.border.color.default.enabled" type="idref" value="Button_Border_Color_Default_Enabled"/>
<defaultsProperty key="EaSynth.button.border.color.default.mouseover" type="idref" value="Button_Border_Color_Default_MouseOver"/>
<defaultsProperty key="EaSynth.button.border.color.default.pressed" type="idref" value="Button_Border_Color_Default_Pressed"/>
<defaultsProperty key="EaSynth.button.border.color.default.disabled" type="idref" value="Button_Border_Color_Default_Disabled"/> 

The "type" attributes of these properties are set to "idref" means the value is linked to another object, and the id of that object is present in the "value" attribute. Let's search the id "Button_Border_Color_Enabled" in the XML and we will see this:

<object id="Button_Border_Color_Enabled" class="java.awt.Color">
    <int>192</int>
    <int>170</int>
    <int>145</int>
    <int>255</int>
</object>

Actually it is a Java bean object that persisted with XML format, the four integer parameter are Red, Green, Blue and Alpha value of the color, which can be 0~255. We can modify this Color object to change the border color for "Enabled" state, we can also change other Color objects to custom the border color for other states as well. The figure below shows the effect of my modification:

New button border color

We can also change the "insets" element under the button style, this element can specify the margin of the button text:

<insets top="4" left="6" bottom="4" right="6"/>

 By specifying different value for "top", "left", "bottom" and "right", we can place the button text on different position, just like these:

Button text positions

It is true that we can replace the "com.easynth.designer.laf.painter.EaSynthPainter" painter class used by EaSynth look and feel, to obtain the full flexibility to paint anything we need on the button, but this topic is out of the scope of this article. You can use the reference resource as the entrance to know more about that.

Reference Resource

EaSynth Look And Feel Homepage: http://www.easynth.com/freewares/EaSynthLookAndFeel.html

Synth Look and Feel Introduction from SUN: http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/synth.html

Synth Look and Feel wiki: http://en.wikipedia.org/wiki/Synth_Look_and_Feel

Synth Look and Feel Javadoc: http://java.sun.com/javase/6/docs/api/javax/swing/plaf/synth/package-summary.html

Synth XML File Format: http://java.sun.com/javase/6/docs/api/javax/swing/plaf/synth/doc-files/synthFileFormat.html

Synth Component Specific Properties: http://java.sun.com/javase/6/docs/api/javax/swing/plaf/synth/doc-files/componentProperties.html

Synth Painter Javadoc: http://java.sun.com/javase/6/docs/api/javax/swing/plaf/synth/doc-http://java.sun.com/javase/6/docs/api/javax/swing/plaf/synth/SynthPainter.html

 

Published at DZone with permission of its author, Xavier Young.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)