Renderer cell in JXTreeTable in base of content of a rows in java

454 Views Asked by At

I'm using JXTreeTable from org.jdesktop.swingx to display a hierarchical structure data, so I've a some parent nodes and some child nodes attached to them. What I want is to have different renderer for parent and child nodes.

Example: if JXTreeTable shows a parent node, the first column should be rendered as a String and the others with a JCheckBox else if JXTreeTable displays a child node, every cell should be rendered as a String.

First, I write in myTreeTableModel that extends AbstractTreeTableModel this piece of code:

@Override
public Class<?> getColumnClass(int column) {
    if (column == 0)
        return String.class;
    return Boolean.class;
}

Everything is ok in parent node, but this shows an empty JCheckBox even in child nodes where I would like to have a blank cell. Returning null in method getValueAt(....) doesn't fix the problem. So, I thought to use Renderer class but I don't know where to start. I write a pseudo-renderer like this:

private class CustomCellRender implements TableCellRenderer {
   @Override
   public Component getTableCellRendererComponent(JTable table,
        Object value, boolean isSelected, boolean hasFocus, int row,
        int column) {
   setOpaque(true);
   JComponent component = null;
    if (value instanceof Parent_Node) {
        if (column == 0)
            component = new JLabel();
        else
            component = new JCheckBox();
    } else if (value instanceof Child_Node) {
        component = new JLabel();
    }
    return component;
   }
}

and I set this with:

myTreeTable.setDefaultRenderer(Object.class, new CustomCellRender());

Could anyone tell where I'm wrong or give me some tips to solve this problem? Thank you for your time.

EDIT: I tried to insert a test System.out.println(...) in method getTableCellRendererComponentbut it's never called. So JTreeTable never runs this method.

0

There are 0 best solutions below