Java Synth is a component in Swing toolkit, which is able to provide a configurable, nicer interface and user experience as it allows customizing the look and feel of Java by one created by Synth.
Synth is available since its J2SE 5.0 version, and it can be configured through a xml document, where the style of app’s elements are defined.
Example of xml setting:
<synth> <style id=”estiloEjemplo”> <font name=”Arial” size=”11” style=”BOLD”/> <color type=”BACKGROUND” value=”#000000”/> <color type="FOREGROUND" value="BLACK" /> </style> <!—Aqui le aplicamos el estilo creado a un textfiled --> <bind style="estiloEjemplo" type="name" key="miTextFieldCreado" /> </synth>
Once the xml document that contains the Synth styles is created, we must load it to the context of the application through load method of SynthLookAndFeel class, as following example.
SynthLookAndFeel lookAndFeel = new SynthLookAndFeel(); lookAndFeel.load(new File("synthExample.xml").toURL()); UIManager.setLookAndFeel(lookAndFeel);
Something we should think about is that Synth does not have a default style, and it does not apply any style to the components that are not defined in our xml document. Anyway, you can have a part of the application with a default style and another one with a configurable style by Synth .This is because Synth allows you to assign a particular style to a component by its name and also allows you to assign a style to a set of components of the same type via style inheritance (similar to how it would apply in CSS).
The way to apply a inheritance style through Synth is by creating a style and associate it to the name of the class that identifies the element. For example, we can have a style for an combo box element, in particular or have a general style for all the combo box of the application.
<!—Estilo de un combo box --> <style id="miComboStyle"> <font name="Arial" size="11" /> <insets top="2" bottom="1" left="4" right="1" /> <state value="ENABLED"> <color value="#000007" type="TEXT_FOREGROUND" /> </state> <state value="SELECTED"> <color type="TEXT_FOREGROUND" value="#FFFFFF" /> <color type="TEXT_BACKGROUND" value="#BEA08C" /> </state> <state value="DISABLED"> <color value="#CCCCCC" type="BACKGROUND" /> <color value="#EFECEC" type="TEXT_FOREGROUND" /> </state> </style> <!—Aquí le aplicamos el estilo creado a todos los combo box de la aplicación, por herencia de estilos--> <bind style="miComboStyle" type="region" key="ComboBox" />
Advantages and disadvantages of Synth
One advantage that it has, is that through the inheritance of styles, as we previously mentioned, one can define a style and apply it to all the components of the application, in this way we will be able to save code.
Another advantage of the styles created through Synth, is that the styles behave the same way in different platforms.
As negative aspects we could observe that there are not much documents or user experiences of it in the net, besides the one given by Oracle in its website referred to Java Synth.
Another disadvantage is that if you only work with one xml configuration document, this one could grow exponentially, helping for it to become difficult to understand and unmaintainable.