2012-11-11 170 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