目前找到的方法只有重写
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){}
这个方法,
然后做判断。 可是我现在需要从数据库中取值进行判断哪个节点需要哪个图标,
现在发现,树生成后,每次展开或关闭,时getTreeCellRendererComponent都会执行,那样就会进行很多很多次的查找数据库有什么好办法一次生成树后,不用在查数据库呢?

解决方案 »

  1.   

    既然这样的话,那你可以定义一长驻内存的Collection,
    系统启动时,将你所需要的图标对应的节点数据从数据库中读取出来,
    并add到Collection中,当然,在用户操作树的过程中,你也必须得
    对该Collection进行相应的add 或者remove,在此过程中,你树的
    展开或者关闭都遍历该Collection即可
    即:
    class YourClass{
    private static Collection treeIconList = null;
    static{
       treeIconList = new ArrayList();
       //or new Vector etc
       //and load the icon-data into the treeIconList(only once)
    }
    //here,rewrite the method getTreeCellRendererComponent()...
    //good luck to you
    }
      

  2.   

    放到硬盘中,通过xml文件保存版本对比信息(是否是最新状态,是否状态有所改变)建议再界面生成的时候就把所有的东西都做好,达到动态生成(面板生成时),静态调用(面板点击时)的效果.