Creating Named Styles Using Java's Synth Look-and-Feel

| No Comments | No TrackBacks

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.

 

- <style id="WizardBg">
  <object id="background" class="com.ssl.cctv.tvbs.oif.gui.swing.skin.BackgroundPainter" />
- <object class="javax.swing.plaf.ColorUIResource" id="startColor">
  <int>108</int>
  <int>171</int>
  <int>221</int>
  </object>
  <defaultsProperty key="Panel.startBackground" type="idref" value="startColor" />
- <object class="javax.swing.plaf.ColorUIResource" id="endColor">
  <int>184</int>
  <int>196</int>
  <int>197</int>
  </object>
  <defaultsProperty key="Panel.endBackground" type="idref" value="endColor" />
  <painter method="panelBackground" idref="background" />
  </style>
  <bind style="WizardBg" type="name" key="wizardPanel" />

 

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.

Related Entries

No TrackBacks

TrackBack URL: http://jenikya.com/cgi-bin/mt5/mt-tb.cgi/56

Leave a comment

Archives

OpenID accepted here Learn more about OpenID