[英]Custom JTree Renderer - Support both CheckBoxes and Default
我正在尝试创建一个自定义 JTree 渲染器,它可以显示带有彩色文本的 JChckBoxes 和 JLabel。
当我使用该渲染器创建 JTree 时,它显示所有节点都是乱码。
下面是我的代码。 不知道我做错了什么。 如果我删除彩色文本和普通文本,只保留 JCheckbox 或只使用彩色文本,它工作正常。
感谢任何帮助。
我的渲染器 class:
public class CheckBoxCellRenderer extends JPanel implements TreeCellRenderer {
public CheckBoxCellRenderer() {
super();
this.setLayout(new BorderLayout());
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
Object obj = node.getUserObject();
if(obj instanceof JCheckBox){
JCheckBox checkBox = (JCheckBox) obj;
//TreePath tp = new TreePath(node.getPath());
if(hasFocus)
{
checkBox.setBackground(Color.darkGray);
checkBox.setForeground(Color.white);
} else {
checkBox.setBackground(Color.white);
checkBox.setForeground(Color.black);
}
add(checkBox, BorderLayout.CENTER);
setOpaque(false);
this.validate();
System.out.println("JCheckBox");
return this;
} else if(obj instanceof ColoredString1){
JLabel label = new JLabel();
ColoredString1 cString = (ColoredString1) obj;
if(hasFocus)
{
label.setBackground(Color.gray);
label.setForeground(cString.getColor());
} else {
label.setBackground(Color.white);
label.setForeground(cString.getColor());
}
label.setText(cString.getValue());
add(label, BorderLayout.CENTER);
this.validate();
System.out.println("ColoredString1");
return this;
} else {
JLabel label = new JLabel();
if(hasFocus)
{
label.setBackground(Color.gray);
} else {
label.setBackground(Color.white);
}
label.setText((String) obj);
add(label, BorderLayout.CENTER);
this.validate();
System.out.println("Default");
return this;
}
}
}
我的主要代码示例:
public class JPOC_JTree_Custom {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame myframe = new JFrame("Custom JTree POC");
myframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JTree myTree = new JTree();
myTree.setCellRenderer(new CheckBoxCellRenderer());
//((DefaultMutableTreeNode)((DefaultTreeModel)myTree.getModel()).getRoot()).removeAllChildren();
DefaultTreeModel model = (DefaultTreeModel) myTree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
root.removeAllChildren();
root.setUserObject("Root_Test");
DefaultMutableTreeNode TreeNode_Ex_1 = new DefaultMutableTreeNode(new ColoredString1("Red", Color.red));
DefaultMutableTreeNode TreeNode_Ex_2 = new DefaultMutableTreeNode(new ColoredString1("Orange", Color.orange));
DefaultMutableTreeNode TreeNode_Ex_3 = new DefaultMutableTreeNode(new ColoredString1("Green", Color.green));
DefaultMutableTreeNode TreeNode_Ex_4 = new DefaultMutableTreeNode(new ColoredString1("Blue", Color.blue));
DefaultMutableTreeNode TreeNode_Ex_5 = new DefaultMutableTreeNode("5");
DefaultMutableTreeNode TreeNode_Ex_6 = new DefaultMutableTreeNode(new JCheckBox("Check?"));
root.add(TreeNode_Ex_1);
root.add(TreeNode_Ex_2);
root.add(TreeNode_Ex_3);
root.add(TreeNode_Ex_4);
root.add(TreeNode_Ex_5);
root.add(TreeNode_Ex_6);
//model.setRoot(root);
model.reload(root);
//myTree.setModel(model);
JScrollPane myTree_ScrollPane = new JScrollPane();
myTree_ScrollPane.setViewportView(myTree);
myframe.add(myTree_ScrollPane);
myframe.validate();
myframe.setVisible(true);
System.out.println("Done");
}
}
--
正如@VGR 确认的那样,在添加 removeAll 之前添加为我解决了这个问题。
removeAll();
add(label, BorderLayout.CENTER);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.