When a JTree node is edited, the node’s user object is lost (set to String). How do I get the original user object? Because I would to like to get the user object’s id so as to update the edited name in the database.
I am using TreeModelListener’s treeNodesChanged method as shown below.
public void treeNodesChanged(TreeModelEvent e) {
DefaultMutableTreeNode node;
node = (DefaultMutableTreeNode) (e.getTreePath().getLastPathComponent());
try {
int index = e.getChildIndices()[0];
node = (DefaultMutableTreeNode) (node.getChildAt(index));
} catch (NullPointerException ex) {}
System.out.println(node.getUserObject().getClass()); // my user object gone
}
So is there any way where I can get the original user object before edit? Any DefaultTreeCellRenderer or DefaultTreeCellEditor methods to override?
Implement TreeModel.valueForPathChanged(). Or override DefaultTreeModel.valueForPathChanged() if you’re using
DefaultTreeModel. This is where the new user object is set and it gives you the opportunity to get to the original user object.