I am implementing TreeModel
interface and have implemented all methods except for the valueForPathChanged
.
In my case, the values are not going to be programatically changed.
Why TreeModel interface contains this method? Is it used by JTree
in any circumstances, or I am safe to leave it unimplemented?
Code for the reference, it works. I am just concerned whether valueForPathChanged
is required by the JTree:
class ParamsTreeModel implements TreeModel {
private final TreeRoot root;
private final List<TreeModelListener> listeners = new ArrayList<TreeModelListener>();
ParamsTreeModel(TreeRoot root) {
this.root = root;
}
@Override
public void addTreeModelListener(TreeModelListener l) {
listeners.add(l);
}
@Override
public Object getChild(Object parent, int index) {
if(parent instanceof Param) return null;
if(structuredMap.containsKey(parent)) {
return structuredMap.get(parent).get(index);
}
// Root
return partNames.get(index);
}
@Override
public int getChildCount(Object parent) {
if(parent instanceof Param) return 0;
if(parent instanceof TreeRoot) return partNames.size();
return structuredMap.get(parent).size();
}
@Override
public int getIndexOfChild(Object parent, Object child) {
if(parent instanceof TreeRoot) return partNames.indexOf(child);
return structuredMap.get(parent).indexOf(child);
}
@Override
public Object getRoot() {
return root;
}
@Override
public boolean isLeaf(Object node) {
return (node instanceof Param);
}
@Override
public void removeTreeModelListener(TreeModelListener l) {
listeners.remove(l);
}
@Override
public void valueForPathChanged(TreePath path, Object newValue) {
// TODO Auto-generated method stub
}
}
While you are required to implement the valueForPathChanged()
method, as defined in the TreeModel
interface, you are free to leave it empty. FileTreeModel
, cited here, is an example. The valueForPathChanged()
method is typically used to support cell editing. As a concrete example, the implementation in DefaultTreeModel
, seen here, "sets the user object of the TreeNode
identified by path
and posts a node changed [event]."