Skip to main content

Embedded Objects in Synth Look And Feel

Posted by xuanyun on January 28, 2009 at 6:36 AM PST

As we know, Synth look and feel can be customized with an XML file which satisfies
the Synth
XML format
. After we go through the document, we can conclude that the type
of property in the XML can be one of the following:

  • idref
  • boolean
  • dimension
  • insets
  • integer
  • string

Actually the idref is the most important one, since it is
a type that represent a reference of object, which may be the instance of any
specific class. In fact we can use idref only to specify any property, but in
order to simplify the XML data, other types are also provided.

The string type is not supported in JRE5, it is shipped with
JRE6. So please be careful, if you want to develop a look and feel that can
work under JRE5, you’d better not to use the string property
type, you can use the idref type instead. BTW, I do think the
Synth look and feel in JRE5 is too buggy, we worked a lot to make EaSynth
look and feel
to work properly under JRE5. So I really suggest to develop
Synth based look and feel that can run under JRE6+ only, that will save a lot
of time.

The idref is a reference of existing object, its value should
be the id assigned to the object. There are several kinds of objects which can
be defined in the XML(they all have id attribute):

  • style
  • state
  • font
  • color
  • insets
  • imagePainter
  • imageIcon
  • beansPersistance entity

To embed the first 7 kinds of objects into the XML, you should follow the Synth
File Format
document, but for the beansPersistance entity,
the document did not describe it clearly. Actually the beansPersistance
entity
is very important, it can embed any object into the XML. Of
course it is not a Base64 solution ;-), it is using the XML serialize instead.
You can use the java.beans.XMLEncoder class to generate the
XML snippet to represent the object. As example, you can view the "easynth.xml"
file in EaSynth
look and feel
, you can see something like these:

beansPersistance entity for Color object:

<object id="Button_Border_Color_Enabled" class="java.awt.Color">
<int>192</int>
<int>170</int>
<int>145</int>
<int>255</int>
</object>

beansPersistance entity for customized Painter
object:

We can see these objects all have their ids, so that they can be referenced
in the property in XML.

Related Topics >>

Comments

Hi, Thank you for the reply. As you said, the custom renderer looks for values from the synth file. The icon issues are fixed. where should the error logs be checked... is it in java consoles? Facing another issue with the background colors to the cell. The custom renderer has a default jlabel component. i am trying to bring a different background color to parent node and leaf node. The parent node color should fill the entire width and height designated for that node, (say for eg., the parent nodes in a tree should always have a black background and the leaf node should have a grey background). tried extending SynthPainter class and overide the paintTreeCellBackground method. The parent node has a black background color set. but when the focus switches to the leaf node, the background color of the parent node is not retained. hope i am clear. Regards.

Hi, Can you please point me to documents which would help me in designing the UI with Synth L&F. While integrating the existing java application with a synth look and feel, i am encountering problems with rendering the cells of a tree. The existing application has a customized renderer that renders each cell of the tree with an icon and name. this icon to a node changes dynamically based on the data the node holds. This custom renderer does not work with synth. Can the icons be changed dynamically using the synth style? Kindly assist in this effort. Thanks.

Synth look and feel support custom renderer. In your case, may be the custom renderer depends on some values from default L&F. You may have to check the error log and the renderer source code to find out the solution.

Hello javaxplore, Synth look and feel lacks of document, but you can take a look at these: http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/synth.html (2) WIKI: http://en.wikipedia.org/wiki/Synth_Look_and_Feel (3) Synth XML File Format: http://java.sun.com/javase/6/docs/api/javax/swing/plaf/synth/doc-files/s... (4) Synth Component Specific Properties: http://java.sun.com/javase/6/docs/api/javax/swing/plaf/synth/doc-files/c...

Synth L&F will automatically set the name of the tree cell renderer component to "Tree.renderer ", but it is not what you need. Since you want two renderer styles, but there is only one name. Actually I prefer not using Synth style to customze the tree cell look, instead customze the renderer with java code. Since the renderer is a JComponent, you can customize its look with java code. You can call setForeground() or setBackground() methods. You can even override the paintComponent() method of the renderer component to do what you want.

Hi, is it Tree.renderer for which the style has to be set? The component specific properties doc says "JTree sets the name of the renderer to Tree.renderer" is the understanding correct? if so, how to set styles in synth xml file? Thank you.

Hi Xuan Yun, Can you give an insight into how to set style for a renderer in the synth xml file? Thank you.

The tree cell is not a JComponent, but the renderer is. I think the most simple way is to set the style to the renderer component directly. You can call the setForeground() or setBackground() method to change the color of the tree cell, you can even override the paintComponent() method of renderder to paint anything you want on the tree cell. If you stick to use Synth to achieve this, you can get the current painting JComponent from the SynthContext in paintTreeCellBackground() method, then check its name, if it is tree1, you paint an icon, if is tree2, you paint a checkbox... It is kind of hacking and I don't really recommend this.

Hi Xuan Yun, Thank you for your response. i havent understood on how to go about it. have two renderers one an Icon Renderer and the other is a CheckBox Renderer. Say Tree1 uses the Icon renderer and Tree2 uses a checkbox renderer. Now the l&f of Tree1 is set through synth by binding it by name "Tree1" and tree2 with name "Tree2". so able to set different background style for these two trees. But the region TreeCell style definition in synth xml file is one. so the cells in both the tree take the same style. this treecell style points to paintTreeCellBackground method that is overridden. yes, you were right that treeCell is not a component. how to go about it? hope i am clear Thank you.

Hi javaxplore, since the tree cell is not a JComponent, you can not assign the name for them. Putting the name to the tree object works but thus you can not have different styles for different nodes... The solution may be using different renderer components for different nodes. You can override the getTreeCellRendererComponent() method to return the renderer object according to the painting node. Of course you can define some JLabel styles and bind them to the renderers by name.

Hi Xuan Yun, have another question on tree. how to bind style by name for the TreeCell ? Thank you.

Hi, Fixed this issue. it is the paintTreeBackground method of the SynthPainter class that should be overridden for a l&f of the tree. Thanks

Hi, Thank you. The tree with the custom renderers works fine. Regards.

You can try the "Tree.drawHorizontalLines" and "Tree.drawVerticalLines" properties.

Hi Xuan Yun, When using Synth L&F, how to hide horizontal and vertical lines between nodes? The putClientProperty("JTree.lineStyle", "None"); works only in Metal L&F. Thanks