I'm learning to JTrees and Java.
Constructive suggestions and feedback are very welcome.
I think I am missing some understanding of JTrees and after 5 hours Googling, and testing I'm stuck. I've simplified the code as much as possible.
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) When adding a new node into the JTree the code throws Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
2) Any general constructive feedback very welcome.
Kind regards
The problem is not the startEditingPath, but the incorrect model implementation. Basically its fails to notify its listeners on changes, consequently the ui has no chance to update its internals to include the added node.
The model fails in
It's not entirely trivial and the java doc slightly (to put it mildly) confusing - that's why SwingX has a well-tested utility class TreeModelSupport which takes over the burden. It can be used standalone, or as a blue-print of how-to do it.
In your custom model, some relevant changes (incomplete, the other modification methods must be fixed accordingly, as must the remove listener):
// 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()]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With