EaSynth Look And Feel Customization (1)
Join the DZone community and get the full member experience.
Join For Freeeasynth 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.
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:
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:
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:
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:
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:
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:
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
Opinions expressed by DZone contributors are their own.
Comments