tags:

views:

32

answers:

2

Hi I have cretaed my own renderer. I want the back ground should be blue. I have set background color as blue also. But I donot know what is tha problem that the background color of my renderer always seems to be white.

I have post the code. please help where I am wrong so that the background color becomes white.

class CheckTreeCellRenderer extends JPanel implements TreeCellRenderer {

private CheckTreeSelectionModel selectionModel;
private MyRenderer delegate;
private TristateCheckBox checkBox = new TristateCheckBox("", null, true);
public static final State NOT_SELECTED = new State();
public static final State SELECTED = new State();
public static final State DONT_CARE = new State();

public CheckTreeCellRenderer(MyRenderer delegate, CheckTreeSelectionModel selectionModel) {
    this.delegate = delegate;

    this.selectionModel = selectionModel;
    setLayout(new BorderLayout());
    setOpaque(true);
    setBackground(new Color(207, 219, 234));
    checkBox.setState(Boolean.TRUE);

    checkBox.setOpaque(true);
    checkBox.setBackground(new Color(207, 219, 234));
}

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
        boolean leaf, int row, boolean hasFocus) {
    Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);


    TreePath path = tree.getPathForRow(row);

    if (path != null) {
        if (selectionModel.isPathSelected(path, true)) {
            checkBox.setState(Boolean.TRUE);
        } else {
            checkBox.setState(selectionModel.isPartiallySelected(path) ? null : Boolean.FALSE);
        }
    }

    renderer.setBackground(new Color(207, 219, 234));
    tree.setOpaque(true);
    tree.setBackground(new Color(207, 219, 234));
    this.setOpaque(true);
    this.setBackground(new Color(207, 219, 234));

    add(checkBox, BorderLayout.WEST);
    add(renderer, BorderLayout.CENTER);

    return this;
}

}

A: 

Don't you have any exception raised ? And are ou sure your getTreeCellRendererComponent method gets called at least one ?

Riduidel
No, I didnot get any exception
Deepak
And have you checked that the method is correctly called ?
Riduidel
I have checked that method is correctly called
Deepak
+1  A: 

Hard to tell without seeing the rest of the code.

I guess that the delegate's renderer most likely contains an opaque component with a white background. The code only sets the renderer blue, the renderers contained components (if it has any) are not adjusted by the above code.

Keilly