0
我爲一個JTree文件編寫了一個樹渲染器,它可以在第一次創建樹時設置背景顏色,但不會突出顯示選擇。JTree TreeCellRenderer不突出顯示選擇
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;
}
}
加入
super.getTreeCellRendererComponent
解決了這個問題。