嗨,我已经创建了我自己的渲染器。我希望背景应该是蓝色的。我也将背景颜色设置为蓝色。但我不知道是什么问题,我的渲染器的背景颜色似乎总是白色。
我已经发布了代码。如果我错了,请帮我把背景颜色改成白色。
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;
}}
发布于 2010-03-16 05:36:10
如果不看剩下的代码,就很难判断。
我猜代理的渲染器很可能包含一个白色背景的不透明组件。代码仅将渲染器设置为蓝色,渲染器包含的组件(如果有)不会被上面的代码调整。
发布于 2010-03-15 22:35:17
你没有提出任何例外吗?你确定你的getTreeCellRendererComponent方法至少被调用了一个吗?
https://stackoverflow.com/questions/2447773
复制相似问题