我正在学习 JTrees 和 Java。
非常欢迎建设性的建议和反馈。
我想我缺少对 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)
1) 当向 JTree 添加新节点时,代码在线程“AWT-EventQueue-0”java.lang.NullPointerException 中抛出异常
2) 非常欢迎任何一般性的建设性反馈。
亲切的问候