jTree 刷新问题 终极解决方法

 案例:

      public void addTreeNode(String userName) {
        if(userName.equals("")||userName==null) {
            System.out.println("节点名为空或是");
        }else {
            javax.swing.tree.DefaultMutableTreeNode subMem=new javax.swing.tree.DefaultMutableTreeNode(userName);
            this.root.add(subMem);            
             this.tree.updateUI();//刷新并展开树节点
                     
        }       
    }

 

在上面的函数中的updateUI方法进行多线程操作的时候,会抛出N多异常,

http://localhost:8010/index.asp4
递归分析开始:.....[2008-11-18 10:27:51]
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at javax.swing.plaf.basic.BasicTreeUI.paintRow(BasicTreeUI.java:1498)
        at javax.swing.plaf.basic.BasicTreeUI.paint(BasicTreeUI.java:1210)
开始分析链接地址:localhost:8010/index.asp
开始分析链接地址:localhost:8010/gsjs.asp
localhost:8010/gsjs.asp:没有新的链接...
线程处理..........................public/update2.asp
线程处理..........................showgoods.asp?id=5297383
线程处理..........................showgoods.asp?id=5297357
线程处理..........................showgoods.asp?id=5294351
线程处理..........................showgoods.asp?id=5294604
线程处理..........................showgoods.asp?id=5295874
线程处理..........................showgoods.asp?id=5295794
线程处理..........................showgoods.asp?id=5294793
开始分析链接地址:localhost:8010/hsxm.asp

  。。。。。。。。。。。。。。。。。。。。。

 

但运行的结果是没问题的;但有没有什么解决方法?

  在网上找到n多类似的问题,都没有解决的方法,后来终于在国外的一个网站上找到解决的方法。

 

这是作者解决的原文:

Post a small demo code that is generally compilable, runnable and could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html and this wiki.

We don't see code for your makeDirectory() method and those after it. So we can only talk a general discourse below:

For updateUI() method, which is irrelevant here, read the API documentation of JComponent class.

Having TreeExpansionListener doing initial tree construction is fundamentally wrong because if it doesn't happen to expand, tree's whole branches also do not exist! -- that could be a cause of subtle bug in future application use. The listener should handle particular business logic or GUI task for the application, not a part of constructor work. Initial tree construction should be done at the root node instantiation in the tree constructor.

If you have made and added a new child node to an existing tree node in JTree runtime, you should call defaulttreemodel.nodeStructureChanged() method on the exixting node and tree.expandPath() for the currently selected treepath.

Hope this helps.

P.S. Why do you NOT make a full file system tree here? If you make it, simple recursion could be used.

 

注意我中间加粗的部分:

   如果您已经和增加了一个新的子节点到现有的树节点在JTree运行时,你应该在存在的节点上调用defaulttreemodel.nodeStructureChanged( )方法和tree.expandPath ( )方法用于当前选定的treepath 。
 
所以,我将上面的代码稍做改动......
 

  public void addTreeNode(String userName) {
        if(userName.equals("")||userName==null) {
            System.out.println("节点名为空或是");
        }else {
            javax.swing.tree.DefaultMutableTreeNode subMem=new javax.swing.tree.DefaultMutableTreeNode(userName);
            this.root.add(subMem);
            this.tree_model.nodeStructureChanged(this.root);            
            this.tree.expandPath(this.tree.getAnchorSelectionPath());                        
        }       
    }

结果就出现了喜剧性的变化,一年以来的问题终于解决;

http://localhost:8010/index.asp4
递归分析开始:.....[2008-11-18 10:25:43]
开始分析链接地址:localhost:8010/index.asp
开始分析链接地址:localhost:8010/gsjs.asp
localhost:8010/gsjs.asp:没有新的链接...
线程处理..........................public/update2.asp
线程处理..........................showgoods.asp?id=5297383
线程处理..........................showgoods.asp?id=5297357
线程处理..........................showgoods.asp?id=5294351
线程处理..........................showgoods.asp?id=5294604
线程处理..........................showgoods.asp?id=5295874
线程处理..........................showgoods.asp?id=5295794
线程处理..........................showgoods.asp?id=5294793
开始分析链接地址:localhost:8010/hsxm.asp
线程处理..........................?page=8
线程处理..........................?page=2
线程处理..........................?page=1
线程处理..........................showgoods.asp?id=5296035
线程处理..........................showgoods.asp?id=5296085
线程处理..........................showgoods.asp?id=5296295
...................................

 

一路跑下来,平平安安的...OK

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值