您好我已经创建了自己的渲染器。我希望背景应该是蓝色的。我也将背景颜色设置为蓝色。但我不知道我的渲染器的背景颜色似乎总是白色的问题是什么。
我发布了代码。请帮助我错了,以便背景颜色变白。
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;
}
}
答案 0 :(得分:1)
很难说没有看到其余的代码。
我猜委托的渲染器很可能包含具有白色背景的不透明组件。代码只将渲染器设置为蓝色,渲染器包含的组件(如果有的话)不会被上面的代码调整。
答案 1 :(得分:0)
你有没有提出任何例外?
你确定你的getTreeCellRendererComponent
方法被调用了至少一个吗?