Skip to main content

Synth Week, Custom Lines Style

Posted by gfx on September 21, 2005 at 11:41 PM PDT

Every Swing look and feel relies on properties to customize the rendering. One of these properties allows you, With BasicLookAndFeel and therefore Metal and Ocean, to change JTree's lines style from solid to dashed:

UIManager.put("Tree.lineTypeDashed", Boolean.TRUE);

The following screenshot shows the difference between dashed tree lines on the left and regular solid tree lines on the right.


Fig. 1. Regular and dashed lines style in a JTree.

Although not vital, this feature was used from time to time and was unfortunately unsupported by Synth. Actually, bug #6258272 reporting this problem had even 3 votes. As part of Mustang b53, this bug is now fixed but instead of just supporting dashed lines we went a step further and added an opportunity for later enhancements. The following snippets enables dashed tree lines in Synth:

<style id="treeStyle">
  <property key="Tree.linesStyle" type="string" value="dashed" />
</style>

As you can see, the property used in Synth is more generic than BasicLookAndFeel. Even though only two styles are supported, dashed and the default one which is solid, you can easily create your own. Indeed, whenever you declare a Synth style (which you later bind to a region or a component by its name), you can provide a graphics utilities entity:

<style id="myStyle">
  <object class="MyGraphicsUtils" id="graphics" />
  <graphicsUtils idref="graphics" />
</style>

The specified class, in this case MyGraphicsUtils, derives from javax.swing.plaf.synth.SynthGraphicsutils and provides several helper methods Synth invokes to render components. One of these methods is drawLine():

void drawLine(SynthContext context, Object paintKey, Graphics g,
              int x1, int y1, int x2, int y2)

The paintKey identifies the type of line. A tree will for instance draw lines of type "Tree.horizontalLine" and "Tree.verticalLine". Since this method is invoked by several UI delegates, the paintKey must be used to change the behavior according not only to the component but also to which line is being drawn. Mustang b53 adds new drawLine() method with the following signature:

void drawLine(SynthContext context, Object paintKey, Graphics g,
              int x1, int y1, int x2, int y2, Objet styleKey)

A new parameter called styleKey identifies the requested style of the line being drawn. In current implementation the only special style supported is "dashed", and only for horizontal (y1 == y2) and vertical lines (x1 == x2). Oblique lines are drawn with the default solid style. You must also know that only JTree will benefit from this new method. If it proves to be useful, we will gradually add support of styled lines to other components as well. Nevertheless, the styleKey gives you a new degree of freedom; you can for instance use it to paint curved lines in a tree as in the following example (screenshot from the chat demo of JavaOne 2005 session Extreme GUI Makeover):


Fig. 2. Curved lines style.

Happy styling!

Related Topics >>