gpt4 book ai didi

java - 当用户在我的 JTree 中编辑节点并单击远离节点时获取新的用户对象字符串

转载 作者:行者123 更新时间:2023-11-29 03:36:14 26 4
gpt4 key购买 nike

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

最佳答案

要稍微改善这种情况,您可以设置 JTree 的 invokesStopCellEditing 属性:为真时,用户界面将提交对某些内部更改(如扩展或选择更改)的待定编辑。

    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();
}
}
}
}

关于java - 当用户在我的 JTree 中编辑节点并单击远离节点时获取新的用户对象字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15391906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com