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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20691946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:37:25  来源:igfitidea点击:

Set icon to each node in Jtree

javaswingjtreetreenodetreecellrenderer

提问by Vlad

I want to set for each node in my JTree a different icon, actually I'm loading each node from a data base, with a "while", I set each icon like a root, leaf or parent. Like this:

我想为我的 JTree 中的每个节点设置一个不同的图标,实际上我正在从数据库加载每个节点,使用“while”,我将每个图标设置为根、叶或父级。像这样:

All my declarations are global:

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

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

And this is the code where I set my nodes:

这是我设置节点的代码:

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*/
}

The method makeNode is this:

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

After to fill the treemodel with my nodes, I set the model to my JTree:

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

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

But the problem is. when I try to set the icons. I create a subclass called myTreeRenderer, and I use this:

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

myJTree.setCellRenderer(new treeRenderer());

But it doesn't set the icons as I want, the subclass is:

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

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

How I can set the icon for each node without using his name? The code of the subclass, as is, set all the nodes with the same icon, and each time I selected a node in the jtree, the getTreeCellRendererComponent runs, I don′t want this.

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

采纳答案by alex2410

You can change default UI values for icons of JTreenodes without any custom renderer:

您可以在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);

or use something like next:

或使用类似下一个:

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

Also read about rendering mechanism.

另请阅读渲染机制

回答by mesutpiskin

You can use it, a shorter way. "tree" is my JTree component.

你可以使用它,一种更短的方式。“树”是我的 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);