简体   繁体   English

将背景色设置为TreeCellRenderer时出现问题

[英]Problem in setting backgound color to my TreeCellRenderer

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;
}

} }

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. 该代码仅将渲染器设置为蓝色,上述代码未调整渲染器包含的组件(如果有)。

Don't you have any exception raised ? 您没有提出任何例外情况吗? And are ou sure your getTreeCellRendererComponent method gets called at least one ? 并且确定您的getTreeCellRendererComponent方法被称为至少一个吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM