TreeModel 接口中的方法 valueForPathChanged (Swing)

Method valueForPathChanged in TreeModel interface (Swing)

我正在实施 TreeModel interface 并且已经实施了除 valueForPathChanged 之外的所有方法。

在我的例子中,这些值不会以编程方式更改。

为什么TreeModel接口包含这个方法?它是否在任何情况下都被 JTree 使用,或者我可以放心地让它未实现?

参考代码,有效。我只是担心 JTree 是否需要 valueForPathChanged

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

    }

}

虽然您需要实施 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,但“设置由 path 标识的 TreeNode 的用户对象并发布更改的节点 [事件]。”