October 2005 Archives
Over the past couple of months, I have been creating my own custom Java look-and-feel (LAF) using Synth. By default, Synth applies the style to an element (JButton, JLabel, JFrame) throughout the application. However, it is often necessary to have an additional style type for an element in the application. For example, I wanted to have a default style for the main application window as well as a different style for the 'wizard'. Most of the elements in the main application used the default font and J-object styles, but I wanted the wizard to have a gradient background colour and section header fonts that were different to the rest of the application.
To create a new style, include a new style in the Synth XML document, as displayed below.
In the 'bind' tag, the type should be defined as 'name' to specify that all elements accepting this style will be bound through the name attribute instead of 'region' which allows all elements of a certain type to be bound to the style. The 'key' attribute in the 'bind' tag will be the name of the element as defined in the Java code. To apply this style to the element, use the myobject.setName("myname") attribute in the Java code. In this example above, the name of the element will be the 'key' attribute in the 'bind' tag: 'wizardPanel'.
If you're a web developer, think of this as a little bit like CSS where you define a style of an element (a font or a DIV) and reference it.
Finally, ensure that all styles with 'named' bindings are located at the end of the XML document so that this style is not overwritten by the default style.
I have been experimenting with developing a custom look-and-feel for a large-scale Java project that I am working on. The custom look-and-feel is in Synth, which loads an XML file defining the styles (and images) for each defined element. Essentially, it really acts like a skin on top of the existing Java Swing interface. Documentation on this is pretty scarce and not in-depth, and I have picked up a lot of inconsistencies and bugs on the way.
In theory, each component consists of different states, such as DEFAULT, MOUSE_OVER, DISABLED, PRESSED, FOCUSED, SELECTED, and ENABLED. (To use more than one state for a style, use the AND keyword between them.) Insets are defined to determine how much space is needed on each side of the element; sourceInsets are used to prevent the area from being stretched. The 'bind style' element is provided to set the defined style. (To determine the key, programmers need to look at Java documentation, which is appended to the end of this entry.) An example of a button element Synth style is below:
<insets top="15" left="20" right="20" bottom="15"/>
<imagePainter method="buttonBackground" path="images/button.png" sourceInsets="10 10 10 10"/>
<bind style="buttonStyle" type="region" key="button"/>
Another important note is that each Java Swing element may inherit attributes from other Java Swing elements. For example, a JList is also made up of a JLabel. This opens up several annoying inconsistencies that I have had to work through with little documentation. The more complex the Swing component, the more likely it inherits from several different Swing components, which will need to be restyled. A range of different components can also be used per component, depending on the state of the component.
Another issue with Synth is that once you use it, you need to create a style for every element. Some of these are very in-depth and require a lot of graphical design work. So, if you just want to change all of your buttons to be a certain colour and theme, Synth is not for you.
Synth Component Properties: http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/plaf/synth/doc-files/componentProperties.html
Synth: The Region Class:
I am currently working on a large-scale Java project as the User Interface Programmer in charge of the graphical user interface. In Java, the layout and aesthetics of an application is known as look-and-feel (LAF). I have had some time on my hands so instead of using the standard Java LAF, I have decided to create my own custom LAF based on the client's branding.
A few developers (including those at Sun) have developed new LAFs for users who want to get away from the default Java LAF. Two of these popular LAFs are known as 'Metal' and 'Ocean', and there are other ones based on the Windows or Mac operating system LAF. For what I had in mind, these LAFs are not much better than the default Java LAF and could not be tweaked to create the LAF that I wanted to use. In order to get a unique non-Java-looking application, I would need to create my own custom LAF.
In my search to find a relevant LAF to use, I came across a new framework which allows developers to create their own LAFs. This custom look-and-feel framework in Java is known as Synth, and code for the framework is written to load an XML file defining the styles (and images) for each defined element (JButton, JLabel, JFrame, etc). Essentially, the Synth framework acts as a skin for the existing Java Swing interface.
Although the Synth LAF framework seems to be a useful way to create a custom LAF, there is a lack of detailed documentation and examples to get started. I also think that in order to create a custom LAF using Synth, the developer must also have graphic design skills. Perhaps it is for this reason that few examples of LAFs created with Synth exist.
Although there is a lack of examples and documentation on the subject, I plan to continue learning about Synth to see how far I get with my custom LAF. If it all fails and there are too many bugs to finish before the project officially kicks off, then I will simply go back to using the default LAF or one of the other popular ones.