2012-11-11 118 views
3


我正在学习JTrees和Java的时候抛出异常。
建设性的意见和反馈都非常欢迎。
startEditingAtPath()将节点添加到一个JTree

我觉得我缺少JTrees的5小时后,谷歌搜索一些了解,并测试我被困。我尽可能简化了代码。

 public void actionPerformed(ActionEvent event) { 
      MyNode selNode = (MyNode) m_tree.getLastSelectedPathComponent(); 
      if (selNode != null) { 
       MyNode newNode = new MyNode("New Node"); 
       model.insertNodeInto(newNode, selNode, 
         selNode.getChildCount()); 
       MyNode[] nodes = model.getPathToRoot(newNode); 
       TreePath path = new TreePath(nodes); 
       m_tree.scrollPathToVisible(path); 
       m_tree.setSelectionPath(path); 
       // ******* The next line throws the exception shown below. **** 
       m_tree.startEditingAtPath(path); 
      } 


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at javax.swing.plaf.basic.BasicTreeUI.startEditing(BasicTreeUI.java:2059) 
at javax.swing.plaf.basic.BasicTreeUI.startEditingAtPath(BasicTreeUI.java:601) 
at javax.swing.JTree.startEditingAtPath(JTree.java:2349) 
at ItemCreator.ItemCreator$1.actionPerformed(ItemCreator.java:74) 

Code - My Simple Mutable JTree

1)当添加新节点插入到JTree的代码抛出异常在线程 “AWT-EventQueue的-0” 显示java.lang.NullPointerException

2)任何一般建设性反馈非常欢迎。

亲切的问候

+2

请在你的问题相关的(而不仅仅是链接)。欲了解更多信息,请参阅:http://codereview.stackexchange.com/faq#make-sure-you-include-your-code-in-your-question –

+0

完成。如果我可以做任何事情,只要问。谢谢你的评论。 – nslntmnx

+2

欢迎,@nslntmnx。请在[so]上发布您的问题,因为Code Review仅适用于工作代码,所以这里是无关紧要的。你的问题结构良好,所以我毫不怀疑你会在那里得到一个很好的答案。当您修正了自己的异常,可随时回到这里,编辑您的问题,以获取有关您的代码的结构的反馈。 – Adam

回答

1

的问题是不是startEditingPath,但不正确的模型实现。基本上,它未能在变更通知其侦听器,因此用户界面也没有机会来更新其内部包括所添加的节点。

该模型在

  1. 不接受听众(空实现addTreeModelListener的)
  2. 甚至没有试图火失败后改变(在插入,更新...)
  3. 不正确getPathToRoot实施

这是不完全琐碎和Java文档略(说得客气一点)困惑 - 这就是为什么SwingX有一个屡试不爽的实用工具类TreeModelSupport接管负担。它可以单独使用,或作为如何操作的蓝图。

在您的自定义模型,一些相关的变化(不完整的,其他的改性方法必须相应固定的,必须删除听众):

// prepare fix issue 1: instantiate the notification support  
private TreeModelSupport support; 

public ItemTreeModel(MyNode root) { 
    this.root = root; 
    support = new TreeModelSupport(this); 
    // remove the following: a model never listens to itself 
    // this.addTreeModelListener(new MyTreeModelListener()); 
} 

// fix issue 1: accept listener 
public void addTreeModelListener(TreeModelListener l) { 
    support.addTreeModelListener(l); 
} 

// fix issue 2: notify the listeners on inserts 
public void insertNodeInto(final MyNode newNode, final MyNode selNode, 
     final int childCount) { 
    selNode.add(childCount, newNode); 
    newNode.setLocation(selNode); 
    support.fireChildAdded(new TreePath(getPathToRoot(selNode)), 
      childCount, newNode); 
} 

// fix issue 3: pathToRoot as needed in TreePath 
public MyNode[] getPathToRoot(MyNode node) { 
    ArrayList<MyNode> itemArrayList = new ArrayList<MyNode>(); 
    // TODO - Add root node ? 
    // yes, certainly - please read the java doc for TreePath 
    while ((node != null)) { // && (node != root)) { 
     itemArrayList.add(0, node); 
     node = node.getLocation(); 
    } 
    return itemArrayList 
      .toArray(new MyNode[itemArrayList.size()]); 
} 
+0

谢谢。清晰的工作解决方案。 – nslntmnx

+0

嗨@kleopatra,你是一个巨大的帮助,一旦前。你到底爱不爱看在此跟进的代码审查? http://codereview.stackexchange.com/questions/45449/correctness-of-the-approach-implementing-the-swing-treemodel – nslntmnx