简体   繁体   English

JTree TreeCellRenderer不突出显示选择

[英]JTree TreeCellRenderer not highlighting selection

I wrote a tree renderer for a JTree of files and it can set the background color when the tree is first made, but it does not highlight selections. 我为文件的JTree编写了一个树渲染器,它可以在第一次创建树时设置背景颜色,但是不会突出显示选择。

private class CustomTreeCellRenderer extends DefaultTreeCellRenderer{
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value,boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus){
        if(value instanceof DefaultMutableTreeNode){
            setText(FileSystemView.getFileSystemView().getSystemDisplayName((File) ((DefaultMutableTreeNode) value).getUserObject()));
            setIcon(FileSystemView.getFileSystemView().getSystemIcon((File) ((DefaultMutableTreeNode) value).getUserObject()));
        }
        super.setBackgroundSelectionColor(Color.BLUE);
        if(selected){
            super.setBackground(Color.gray);
            setForeground(getTextSelectionColor());
        }else{
            super.setBackground(Color.CYAN);
            setForeground(getTextNonSelectionColor());
        }
        this.setOpaque(true);
        return this;
    }
}

adding

super.getTreeCellRendererComponent

solved the problem. 解决了问题。

在getTreeCellRendererComponent内部,第一个调用应该是

super.getTreeCellRendererComponent(arguments);

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

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