将图标设置为Jtree中的每个节点

时间:2013-12-19 21:22:50

标签: java swing jtree treenode treecellrenderer

我想为我的JTree中的每个节点设置一个不同的图标,实际上我是从数据库加载每个节点,使用“while”,我将每个图标设置为root,leaf或parent。像这样:

我的所有声明都是全球性的:

private ResultSet myResultSet;
protected DefaultTreeModel treeModel;
private DefaultMutableTreeNode rootNode,childNode,parent1,parent2;

这是我设置节点的代码:

myResultSet=rtnNodes(); /*Method that returns a RS with my nodes*/
while(myResultSet.next()){
  switch(myResultSet.getInt(1)){  /*The first column is the type of node: root, parent, leaf...*/
    case 0: treeModel = new DefaultTreeModel((rootNode=new DefaultMutableTreeNode(myResultSet.getString(2)))); break;  /*root node*/
    case 1: case 4: parent1 = parent2 = makeNode(rootNode); break;  /*parent node*/
    case 2: makeNode(parent2); break;  /*leaf node*/
    case 3: parent2 = makeNode(parent1); break;  /*sub patern node*/
  } /*makeNode is the method where I create the nodes*/
}

方法makeNode是这样的:

public DefaultMutableTreeNode makeNode(DefaultMutableTreeNode parent){
  //The second column in the RS is the name of the node
  treeModel.insertNodeInto((childNode=new DefaultMutableTreeNode(myResultSet.getString(2))),parent,parent.getChildCount());
  return childNode;
}

在用我的节点填充树模型之后,我将模型设置为我的JTree:

myJTree.setModel(treeModel);
myJTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

但问题是。当我尝试设置图标时。我创建了一个名为myTreeRenderer的子类,我使用它:

myJTree.setCellRenderer(new treeRenderer());

但它没有按我的意愿设置图标,子类是:

private ImageIcon root,parent,leaf;

public myTreeRenderer() {
   root=setIcons(2);  /*setIcons is a method that I dont publish in this post, that helps me to set the path of the icons*/
   parent=setIcons(3);
   leaf=setIcons(4);
}

@Override
public Component getTreeCellRendererComponent(JTree tree,Object value,boolean selected,boolean expanded,boolean leaf,int row,boolean hasFocus){               
   super.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus);
   DefaultMutableTreeNode nodo = (DefaultMutableTreeNode)value;
   TreeNode t = nodo.getParent();
   if(t!=null){
      setIcon(root);
   }
   return this;
}

如何在不使用他的名字的情况下为每个节点设置图标?子类的代码,按原样设置所有节点都有相同的图标,每次我在jtree中选择一个节点,getTreeCellRendererComponent运行,我不想这样。

2 个答案:

答案 0 :(得分:17)

您可以更改JTree个节点图标的默认UI值,而无需任何自定义渲染器:

URL resource = logaff.class.getResource(IMAGE);
Icon icon = new ImageIcon(resource);

UIManager.put("Tree.closedIcon", icon);
UIManager.put("Tree.openIcon", icon);
UIManager.put("Tree.leafIcon", icon);

或使用下一个:

@Override
public Component getTreeCellRendererComponent(JTree tree,
    Object value, boolean selected, boolean expanded,
    boolean leaf, int row, boolean hasFocus) {
        super.getTreeCellRendererComponent(tree, value, selected,expanded, leaf, row, hasFocus);
        DefaultMutableTreeNode nodo = (DefaultMutableTreeNode) value;
        if (tree.getModel().getRoot().equals(nodo)) {
            setIcon(root);
        } else if (nodo.getChildCount() > 0) {
            setIcon(parent);
        } else {
            setIcon(leaf);
        }
        return this;
}

另请阅读rendering mechanism

答案 1 :(得分:3)

你可以使用它,一种较短的方式。 “tree”是我的JTree组件。

DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
Icon closedIcon = new ImageIcon("closed.png");
Icon openIcon = new ImageIcon("open.png");
Icon leafIcon = new ImageIcon("leaf.png");
renderer.setClosedIcon(closedIcon);
renderer.setOpenIcon(openIcon);
renderer.setLeafIcon(leafIcon);