Skip to main content

JXTreeTable rendering

11 replies [Last post]
akis2
Offline
Joined: 2010-06-18

I have started using JXTreeTable. I am using the DefaultTreeTableModel and have implemented a custom AbstractMutableTreeTableNode. It works but I have a problem in custom rendering of treetable.
To give an example, I have extended DefaultTreeCellRenderer and overriden the getTreeCellRendererComponent method. In it I call super.getTreeCellRendererComponent but the information in the tree is lost.
I.e. if I do the following trivial:

public class TestRenderer extends  DefaultTreeCellRenderer{
    @Override
    public Component getTreeCellRendererComponent(JTree jtree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
       return super.getTreeCellRendererComponent(jtree, value, sel, expanded, leaf,row,   hasFocus);
    }
}

and later
jXTreeTable1.setTreeCellRenderer(new TestRenderer());
The node's messages are missing. Is there no tutorial for JXTreeTables?
Could not find a rendering example for this construct. Could you give me a head start please?

Thank you.

Message was edited by: akis2

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
keksi
Offline
Joined: 2010-08-17

Hello all

I have a rendering issue with JXTreeTable and DefaultTreeCellRenderer. I want to change the background color dynamically, depending on some tree node property, but it just doesn't work. I tried it in two ways:

1: Overriding method DefaultTreeCellRenderer.getTreeCellRendererComponent(...)

@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus)
{
MyTreeCellRenderer component = (MyTreeCellRenderer)super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

NodeAbstract node = (NodeAbstract)value;
if (node == null) return component;

if (!sel)
{
// This has no effect at all:
if (!node.fileExists() || !node.canRead())
component.setBackground(ColorBGForNonReadableFile);
else if (!node.canWrite())
component.setBackground(ColorBGForNonWritableFile);
else
component.setBackground(ColorBGForWritableFile);

// This works fine:
if (node.isFolder())
component.setForeground(ColorFGForFolder);
else
component.setForeground(ColorFGForFile);
}
}

2: Overriding method JXTreeTable.prepareRenderer(...):

@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component component = super.prepareRenderer(renderer, row, column);
NodeAbstract node = (NodeAbstract)MainView.this.treeTable.getPathForRow(row).getLastPathComponent();

// ToDo: Setting the background and foreground color here doesn't work for DefaultTreeCellRenderer:
if (row != MainView.this.treeTable.getSelectedRow())
{
if (!node.fileExists() || !node.canRead())
{
component.setForeground(ColorFGForNonReadableFile);
component.setBackground(ColorBGForNonReadableFile);
}
else
{
if (node.isFolder())
component.setForeground(ColorFGForFolder);
else
component.setForeground(ColorFGForFile);

if (!node.canWrite())
component.setBackground(ColorBGForNonWritableFile);
else
component.setBackground(ColorBGForWritableFile);
}
}

return component;
}

Can anybody give me a hint?

Many thanx in advance
Keksi

kleopatra
Offline
Joined: 2003-06-11

The SwingX way of visual decoration is _not_ to subclass/implement the treeTable/cellRenderer.

Instead install a Highlighter/HighlightPredicate pair on the treeTable: the former controls the visual effect, the latter the condition. The mechanism is the same for all collection components, for examples look at the demo on the project home page and/or into the test classes.

Cheers
Jeanette

keksi
Offline
Joined: 2010-08-17

Hi Jeanette

Yes, that works! Many thanx!

Now - if I want to use several different colors (like in the example above), I have to utilize several different ColorHighlighters, each one with an own HighlightPredicate, right?

Cheers
Keksi

kschaefe
Offline
Joined: 2006-06-08

Correct.

Karl

kschaefe
Offline
Joined: 2006-06-08

What are you trying to do with your custom TreeCellRenderer?

Karl

akis2
Offline
Joined: 2010-06-18

Hi,

I wanted to add some icons.

kschaefe
Offline
Joined: 2006-06-08

Icons to where? The table handles (changing them) or the contained data?

Karl

akis2
Offline
Joined: 2010-06-18

The contained data. For the handles I saw that there are methods setClosedIcon etc.in the JXTreeTable

Message was edited by: akis2

kschaefe
Offline
Joined: 2006-06-08

You will want to use the SwingX rendering and not roll your own. Review renderer package. Of particular interest to you should be LabelProvider and IconValue.

Karl

akis2
Offline
Joined: 2010-06-18

I checked the renderer package (http://javadoc.geotoolkit.org/external/swingx/org/jdesktop/swingx/render...)
The wrapping provider seemed as the class to use, but the example is not working. I get the error:
[b]java.lang.StringValue is not public in java.lang; cannot be accessed from outside package[/b]
If I do the following:

IconValue iv = new IconValue() {

public Icon getIcon(Object value) {
String txt = (String) value;

if (txt.equals("Action A")) {
return new ImageIcon(NewJFrame2.class.getResource("resources/actionA.png"));
}
return null;
}
};

DefaultTreeRenderer wp = new DefaultTreeRenderer(iv);
jXTreeTable1.setTreeCellRenderer(wp);

The rendering of the tree is broken as described in my previous post (i.e. node text messages are not displayed at all!)
Also I can not understand how label provider is of use to my case.
Could you please, give some help on this?

Message was edited by: akis2

kleopatra
Offline
Joined: 2003-06-11

> I checked the renderer package
> /org/jdesktop/swingx/renderer/package-summary.html)

look carefully at this package name ...

> The wrapping provider seemed as the class to use, but
> the example is not working. I get the error:
> java.lang.StringValue is not public in java.lang;

... and then just as carefully at that package name ;-)

the wrapping provider sure works, there are more examples in the test package (f.i. RendererVisualCheck and JXTreeTableVisualCheck).

CU
Jeanette

BTW: as you certainly noticed, your code tags are not working - reason is that the brackets need to be [ instead of arrow < Please edit your posts accordingly. Thanks