将图标设置为Jtree中的每个节点 [英] Set icon to each node in Jtree

查看:139
本文介绍了将图标设置为Jtree中的每个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

我的所有声明都是全局的:

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

方法makeNode是这样的:

The method makeNode is this:

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 :

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

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

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

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

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

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

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 节点图标的默认UI值:

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

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

另请阅读渲染机制

这篇关于将图标设置为Jtree中的每个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆