Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当用户在my JTree中编辑一个节点并单击远离该节点时,获取新的用户对象字符串_Java_Swing_Jtree - Fatal编程技术网

Java 当用户在my JTree中编辑一个节点并单击远离该节点时,获取新的用户对象字符串

Java 当用户在my JTree中编辑一个节点并单击远离该节点时,获取新的用户对象字符串,java,swing,jtree,Java,Swing,Jtree,我通过扩展DefaultTreeModel创建了一个自定义TreeModel,因此用户可以重命名我的JTree中的节点。如果我的用户输入一个新名称,然后按enter键,则此操作正常。如果用户没有按enter键,而是离开节点单击,则不会触发我的valueForPathChanged方法,并且我无法获取新字符串。如何在用户不按enter键而不是单击树/面板/框架中的其他位置的情况下获取新用户字符串?您可以添加FocusListener的匿名实例 实施 void focusLost(FocusE

我通过扩展DefaultTreeModel创建了一个自定义TreeModel,因此用户可以重命名我的JTree中的节点。如果我的用户输入一个新名称,然后按enter键,则此操作正常。如果用户没有按enter键,而是离开节点单击,则不会触发我的valueForPathChanged方法,并且我无法获取新字符串。如何在用户不按enter键而不是单击树/面板/框架中的其他位置的情况下获取新用户字符串?

您可以添加FocusListener的匿名实例 实施

void    focusLost(FocusEvent e)  
这会在保存值之前触发,因此不会帮助您获取最后一个值。相反,你应该设置

myTree.setInvokesStopCellEditing(true);
为INVOKES_STOP_CELL_EDITING_属性触发属性更改,该属性

这意味着您需要在树模型中包含以下内容

public void valueForPathChanged(TreePath path, Object newValue)
{
AdapterNode node = (AdapterNode)
path.getLastPathComponent();
node.getDomNode().setNodeValue((String)newValue);
fireTreeNodesChanged(new TreeModelEvent(this,
path));
} 

关于

您可以添加FocusListener的匿名实例 实施

void    focusLost(FocusEvent e)  
这会在保存值之前触发,因此不会帮助您获取最后一个值。相反,你应该设置

myTree.setInvokesStopCellEditing(true);
为INVOKES_STOP_CELL_EDITING_属性触发属性更改,该属性

这意味着您需要在树模型中包含以下内容

public void valueForPathChanged(TreePath path, Object newValue)
{
AdapterNode node = (AdapterNode)
path.getLastPathComponent();
node.getDomNode().setNodeValue((String)newValue);
fireTreeNodesChanged(new TreeModelEvent(this,
path));
} 

关于

要稍微改善这种情况,您可以设置JTree的invokeStopCellEditing属性:如果为true,ui将提交一些内部更改的挂起编辑,如扩展或选择更改

    final JTree tree = new JTree();
    tree.setEditable(true);
    // this will often help (see its api doc), but no guarantee
    tree.setInvokesStopCellEditing(true);
    // a focusListener is **not** helping
    FocusListener l = new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
        }

        @Override
        public void focusLost(FocusEvent e) {
            // this would prevent editing at all
           // tree.stopEditing();
        }

    };
    tree.addFocusListener(l);
    JComponent panel = new JPanel(new BorderLayout());
    panel.add(new JScrollPane(tree));
    panel.add(new JButton("just something to focus"), BorderLayout.SOUTH);
要使用的代码片段还表明focusListener不工作

CellEditorRemover及其在JXTree中的用法正如您所看到的,除了基本上是KeyboardFocusManager的focusOwner属性的侦听器的裸机移除器之外,还有一些需要添加的内容:

/**
 * {@inheritDoc} <p>
 * Overridden to fix focus issues with editors. 
 * This method installs and updates the internal CellEditorRemover which
 * terminates ongoing edits if appropriate. Additionally, it
 * registers a CellEditorListener with the cell editor to grab the 
 * focus back to tree, if appropriate.
 * 
 * @see #updateEditorRemover()
 */
@Override
public void startEditingAtPath(TreePath path) {
    super.startEditingAtPath(path);
    if (isEditing()) {
        updateEditorListener();
        updateEditorRemover();
    }
}


/**
 * Hack to grab focus after editing.
 */
private void updateEditorListener() {
    if (editorListener == null) {
        editorListener = new CellEditorListener() {

            @Override
            public void editingCanceled(ChangeEvent e) {
                terminated(e);
            }

            /**
             * @param e
             */
            private void terminated(ChangeEvent e) {
                analyseFocus();
                ((CellEditor) e.getSource()).removeCellEditorListener(editorListener);
            }

            @Override
            public void editingStopped(ChangeEvent e) {
                terminated(e);
            }

        };
    }
    getCellEditor().addCellEditorListener(editorListener);

}

/**
 * This is called from cell editor listener if edit terminated.
 * Trying to analyse if we should grab the focus back to the
 * tree after. Brittle ... we assume we are the first to 
 * get the event, so we can analyse the hierarchy before the
 * editing component is removed.
 */
protected void analyseFocus() {
    if (isFocusOwnerDescending()) {
        requestFocusInWindow();
    }
}


/**
 * Returns a boolean to indicate if the current focus owner 
 * is descending from this table. 
 * Returns false if not editing, otherwise walks the focusOwner
 * hierarchy, taking popups into account. <p>
 * 
 * PENDING: copied from JXTable ... should be somewhere in a utility
 * class?
 * 
 * @return a boolean to indicate if the current focus
 *   owner is contained.
 */
private boolean isFocusOwnerDescending() {
    if (!isEditing()) return false;
    Component focusOwner = 
        KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    // PENDING JW: special casing to not fall through ... really wanted?
    if (focusOwner == null) return false;
    if (SwingXUtilities.isDescendingFrom(focusOwner, this)) return true;
    // same with permanent focus owner
    Component permanent = 
        KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
    return SwingXUtilities.isDescendingFrom(permanent, this);
}



/**
 * Overridden to release the CellEditorRemover, if any.
 */
@Override
public void removeNotify() {
    if (editorRemover != null) {
        editorRemover.release();
        editorRemover = null;
    }
    super.removeNotify();
}

/**
 * Lazily creates and updates the internal CellEditorRemover.
 * 
 *
 */
private void updateEditorRemover() {
    if (editorRemover == null) {
        editorRemover = new CellEditorRemover();
    }
    editorRemover.updateKeyboardFocusManager();
}

/** This class tracks changes in the keyboard focus state. It is used
 * when the JXTree is editing to determine when to terminate the edit.
 * If focus switches to a component outside of the JXTree, but in the
 * same window, this will terminate editing. The exact terminate 
 * behaviour is controlled by the invokeStopEditing property.
 * 
 * @see javax.swing.JTree#setInvokesStopCellEditing(boolean)
 * 
 */
public class CellEditorRemover implements PropertyChangeListener {
    /** the focusManager this is listening to. */
    KeyboardFocusManager focusManager;

    public CellEditorRemover() {
        updateKeyboardFocusManager();
    }

    /**
     * Updates itself to listen to the current KeyboardFocusManager. 
     *
     */
    public void updateKeyboardFocusManager() {
        KeyboardFocusManager current = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        setKeyboardFocusManager(current);
    }

    /**
     * stops listening.
     *
     */
    public void release() {
        setKeyboardFocusManager(null);
    }

    /**
     * Sets the focusManager this is listening to. 
     * Unregisters/registers itself from/to the old/new manager, 
     * respectively. 
     * 
     * @param current the KeyboardFocusManager to listen too.
     */
    private void setKeyboardFocusManager(KeyboardFocusManager current) {
        if (focusManager == current)
            return;
        KeyboardFocusManager old = focusManager;
        if (old != null) {
            old.removePropertyChangeListener("permanentFocusOwner", this);
        }
        focusManager = current;
        if (focusManager != null) {
            focusManager.addPropertyChangeListener("permanentFocusOwner",
                    this);
        }

    }
    @Override
    public void propertyChange(PropertyChangeEvent ev) {
        if (!isEditing()) {
            return;
        }

        Component c = focusManager.getPermanentFocusOwner();
        JXTree tree = JXTree.this;
        while (c != null) {
            if (c instanceof JPopupMenu) {
                c = ((JPopupMenu) c).getInvoker();
            } else {

                if (c == tree) {
                    // focus remains inside the table
                    return;
                } else if ((c instanceof Window) ||
                        (c instanceof Applet && c.getParent() == null)) {
                    if (c == SwingUtilities.getRoot(tree)) {
                        if (tree.getInvokesStopCellEditing()) {
                            tree.stopEditing();
                        }
                        if (tree.isEditing()) {
                            tree.cancelEditing();
                        }
                    }
                    break;
                }
                c = c.getParent();
            }
        }
    }
}

为了稍微改善这种情况,您可以设置JTree的invokeStopCellEditing属性:如果为true,ui将提交一些内部更改(如扩展或选择更改)的挂起编辑

    final JTree tree = new JTree();
    tree.setEditable(true);
    // this will often help (see its api doc), but no guarantee
    tree.setInvokesStopCellEditing(true);
    // a focusListener is **not** helping
    FocusListener l = new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
        }

        @Override
        public void focusLost(FocusEvent e) {
            // this would prevent editing at all
           // tree.stopEditing();
        }

    };
    tree.addFocusListener(l);
    JComponent panel = new JPanel(new BorderLayout());
    panel.add(new JScrollPane(tree));
    panel.add(new JButton("just something to focus"), BorderLayout.SOUTH);
要使用的代码片段还表明focusListener不工作

CellEditorRemover及其在JXTree中的用法正如您所看到的,除了基本上是KeyboardFocusManager的focusOwner属性的侦听器的裸机移除器之外,还有一些需要添加的内容:

/**
 * {@inheritDoc} <p>
 * Overridden to fix focus issues with editors. 
 * This method installs and updates the internal CellEditorRemover which
 * terminates ongoing edits if appropriate. Additionally, it
 * registers a CellEditorListener with the cell editor to grab the 
 * focus back to tree, if appropriate.
 * 
 * @see #updateEditorRemover()
 */
@Override
public void startEditingAtPath(TreePath path) {
    super.startEditingAtPath(path);
    if (isEditing()) {
        updateEditorListener();
        updateEditorRemover();
    }
}


/**
 * Hack to grab focus after editing.
 */
private void updateEditorListener() {
    if (editorListener == null) {
        editorListener = new CellEditorListener() {

            @Override
            public void editingCanceled(ChangeEvent e) {
                terminated(e);
            }

            /**
             * @param e
             */
            private void terminated(ChangeEvent e) {
                analyseFocus();
                ((CellEditor) e.getSource()).removeCellEditorListener(editorListener);
            }

            @Override
            public void editingStopped(ChangeEvent e) {
                terminated(e);
            }

        };
    }
    getCellEditor().addCellEditorListener(editorListener);

}

/**
 * This is called from cell editor listener if edit terminated.
 * Trying to analyse if we should grab the focus back to the
 * tree after. Brittle ... we assume we are the first to 
 * get the event, so we can analyse the hierarchy before the
 * editing component is removed.
 */
protected void analyseFocus() {
    if (isFocusOwnerDescending()) {
        requestFocusInWindow();
    }
}


/**
 * Returns a boolean to indicate if the current focus owner 
 * is descending from this table. 
 * Returns false if not editing, otherwise walks the focusOwner
 * hierarchy, taking popups into account. <p>
 * 
 * PENDING: copied from JXTable ... should be somewhere in a utility
 * class?
 * 
 * @return a boolean to indicate if the current focus
 *   owner is contained.
 */
private boolean isFocusOwnerDescending() {
    if (!isEditing()) return false;
    Component focusOwner = 
        KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
    // PENDING JW: special casing to not fall through ... really wanted?
    if (focusOwner == null) return false;
    if (SwingXUtilities.isDescendingFrom(focusOwner, this)) return true;
    // same with permanent focus owner
    Component permanent = 
        KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
    return SwingXUtilities.isDescendingFrom(permanent, this);
}



/**
 * Overridden to release the CellEditorRemover, if any.
 */
@Override
public void removeNotify() {
    if (editorRemover != null) {
        editorRemover.release();
        editorRemover = null;
    }
    super.removeNotify();
}

/**
 * Lazily creates and updates the internal CellEditorRemover.
 * 
 *
 */
private void updateEditorRemover() {
    if (editorRemover == null) {
        editorRemover = new CellEditorRemover();
    }
    editorRemover.updateKeyboardFocusManager();
}

/** This class tracks changes in the keyboard focus state. It is used
 * when the JXTree is editing to determine when to terminate the edit.
 * If focus switches to a component outside of the JXTree, but in the
 * same window, this will terminate editing. The exact terminate 
 * behaviour is controlled by the invokeStopEditing property.
 * 
 * @see javax.swing.JTree#setInvokesStopCellEditing(boolean)
 * 
 */
public class CellEditorRemover implements PropertyChangeListener {
    /** the focusManager this is listening to. */
    KeyboardFocusManager focusManager;

    public CellEditorRemover() {
        updateKeyboardFocusManager();
    }

    /**
     * Updates itself to listen to the current KeyboardFocusManager. 
     *
     */
    public void updateKeyboardFocusManager() {
        KeyboardFocusManager current = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        setKeyboardFocusManager(current);
    }

    /**
     * stops listening.
     *
     */
    public void release() {
        setKeyboardFocusManager(null);
    }

    /**
     * Sets the focusManager this is listening to. 
     * Unregisters/registers itself from/to the old/new manager, 
     * respectively. 
     * 
     * @param current the KeyboardFocusManager to listen too.
     */
    private void setKeyboardFocusManager(KeyboardFocusManager current) {
        if (focusManager == current)
            return;
        KeyboardFocusManager old = focusManager;
        if (old != null) {
            old.removePropertyChangeListener("permanentFocusOwner", this);
        }
        focusManager = current;
        if (focusManager != null) {
            focusManager.addPropertyChangeListener("permanentFocusOwner",
                    this);
        }

    }
    @Override
    public void propertyChange(PropertyChangeEvent ev) {
        if (!isEditing()) {
            return;
        }

        Component c = focusManager.getPermanentFocusOwner();
        JXTree tree = JXTree.this;
        while (c != null) {
            if (c instanceof JPopupMenu) {
                c = ((JPopupMenu) c).getInvoker();
            } else {

                if (c == tree) {
                    // focus remains inside the table
                    return;
                } else if ((c instanceof Window) ||
                        (c instanceof Applet && c.getParent() == null)) {
                    if (c == SwingUtilities.getRoot(tree)) {
                        if (tree.getInvokesStopCellEditing()) {
                            tree.stopEditing();
                        }
                        if (tree.isEditing()) {
                            tree.cancelEditing();
                        }
                    }
                    break;
                }
                c = c.getParent();
            }
        }
    }
}


看看JTable中的EditorRemover:您必须在自定义JTree中实现类似的东西。或者使用JXTree,JXTree已经有了它:-也许使用JPOpPhave更简单看看JTable中的EditorRemover:您必须在自定义JTree中实现类似的东西。或者使用JXTree,它已经有了:-可能更简单的是使用JPOpptThat不能可靠地工作编辑器在编辑MM时有焦点。。好啊将尝试查找Editor上的一些信息或删除,但仍然不正确:当编辑开始时,将调用focusLost(假定侦听器已在JTree上注册),并立即停止编辑。虽然模型实现是正确的,但这与问题无关。正如我在回答中已经提到的,尽管在文本中拼写错误,invokeStopCellEditing有一点帮助,但它是不完整的f.I.无法捕捉到树外部的焦点更改。这不可靠编辑器在编辑时具有焦点MM。。好啊将尝试查找Editor上的一些信息或删除,但仍然不正确:当编辑开始时,将调用focusLost(假定侦听器已在JTree上注册),并立即停止编辑。虽然模型实现是正确的,但这与问题无关。正如我在回答中已经提到的,尽管在文本中拼写错误,invokeStopCellEditing有点帮助,但它是不完整的f.I.无法捕捉到树外部的焦点更改。我不认为使用JXTree是我的选择。SetInvokeStopCellEditing可靠吗?这和在我的树模型中将其设置为true一样简单吗?re:JXTree不是一个选项-这就是为什么我包含了来自JXTree的代码,只需添加到自定义树中就可以了,但不保证,我维护的是SwingX,而不是此处显示的代码片段:-b re:InvokeStopCellEditing-请阅读其api文档以了解其合同。它不是模型属性,它如何。。。但是一个视图属性Yok,我已经很久没有在Java或Swing中工作了。尽我所能-可能需要一些时间来消化你写的所有东西。将让你知道它是如何运行的,但可能需要一些时间:即使是移除程序也只有在焦点发生变化时才起作用——如果点击了f.i.菜单项,情况可能就不是这样了。因此,您需要在所有位置手动停止编辑,以便将数据保存回数据库或类似数据库,以防止数据丢失。没有什么可以反对的。。。祝你好运,你击中了收集单元编辑中一个更脏的坑:-我想是的!我希望这将是一个简单的添加lol。我感谢您的帮助和支持。我不认为使用JXTree是我的选择。SetInvokeStopCellEditing可靠吗?这和在我的树模型中将其设置为true一样简单吗
y我包含了来自JXTree的代码,只需添加到自定义树中就可以了。不保证,不过,我维护的是SwingX,而不是此处显示的代码片段:-b re:InvokeStopCellEditing-请阅读其api文档以了解其契约它不是模型属性,它怎么可能。。。但是一个视图属性Yok,我已经很久没有在Java或Swing中工作了。尽我所能-可能需要一些时间来消化你写的所有东西。将让你知道它是如何运行的,但可能需要一些时间:即使是移除程序也只有在焦点发生变化时才起作用——如果点击了f.i.菜单项,情况可能就不是这样了。因此,您需要在所有位置手动停止编辑,以便将数据保存回数据库或类似数据库,以防止数据丢失。没有什么可以反对的。。。祝你好运,你击中了收集单元编辑中一个更脏的坑:-我想是的!我希望这将是一个简单的补充笑。我感谢帮助和支持。