Problem in setting backgound color to my TreeCellRenderer
Posted
by Sunil Kumar Sahoo
on Stack Overflow
See other posts from Stack Overflow
or by Sunil Kumar Sahoo
Published on 2010-03-15T14:21:01Z
Indexed on
2010/03/15
14:39 UTC
Read the original article
Hit count: 264
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;
}
}
© Stack Overflow or respective owner