DefaultTreeModel을 확장하여 사용자 정의 TreeModel을 만들었으므로 사용자가 JTree의 노드 이름을 바꿀 수 있습니다. 내 사용자가 새 이름을 입력 한 다음 Enter 키를 누르면 잘 작동합니다. Enter 키를 누르는 대신 사용자가 노드를 클릭하면 valueForPathChanged 메서드가 실행되지 않고 새 String을 가져올 수 없습니다. 사용자가 입력하지 않고 대신 Tree/Panel/Frame의 다른 곳을 클릭하지 않고 새 사용자 String을 얻으려면 어떻게해야합니까?사용자가 JTree 내의 노드를 편집하고 노드를 클릭 할 때 새 사용자 객체 문자열 가져 오기
답변
처럼 나무 모델 뭔가를 가질 필요가 있다는 것을 의미 INVOKES_STOP_CELL_EDITING_PROPERTY 프로퍼티 변경을, 화재
myTree.setInvokesStopCellEditing(true);
을 설정해야 상황을 약간 개선하면 JTree의 invokesStopCellEditing 속성을 설정할 수 있습니다. 사실, 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);
스 니펫 (play with)은 또한 focusListener가 작동하지 않음을 보여줍니다.
CellEditorRemover 및 JXTree에서의 사용 (당신이 보는대로, 조금 더의 KeyboardFocusManager의 focusOwner 속성에 리스너)-A는 기본적으로있는 (베어 제거제보다 추가 할 수있다 :
/**
* {@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();
}
}
}
}
JXTree를 사용하는 것이 나를위한 옵션이라고 생각하지 않습니다. . setInvokesStopCellEditing은 신뢰할 수 있습니까? 그것은 내 나무 모델에서 사실로 설정하는 것만 큼 간단합니까? – Matt
a) re : JXTree 옵션이 필요하지 않습니다. 그 이유는 JXTree의 코드를 포함 시켰기 때문입니다. 사용자 정의 트리에 추가하는 것이 좋을 것입니다. (보장 할 수는 없지만 SwingX는 여기에 표시된 스 니펫이 아닙니다 :-) b) re : invokesStopCellEditing - 그것의 계약서에 대해 배우기 위해 API 문서를 읽으십시오. (모델 속성이 아니고 어떻게 할 수 있습니까?하지만 뷰 속성입니다.) – kleopatra
그래, Java 또는 Swing에서 너무 오랫동안 작업하지 않았습니다. 최선을 다하면 - 내가 쓴 모든 것을 소화하기까지 시간이 걸릴 수도 있습니다. 어떻게되는지 알 수 있지만 시간이 좀 걸릴 수도 있습니다. – Matt
당신의 FocusListener 의 익명의 인스턴스를 추가하고 값이 마지막 값을 얻기에 도움이되지는 않습니다 때문에 저장하기 전에이 트리거됩니다
void focusLost(FocusEvent e)
구현할 수 있습니다. 대신 당신은
는 당신이
public void valueForPathChanged(TreePath path, Object newValue)
{
AdapterNode node = (AdapterNode)
path.getLastPathComponent();
node.getDomNode().setNodeValue((String)newValue);
fireTreeNodesChanged(new TreeModelEvent(this,
path));
}
감사
이 JTable의에서 EditorRemover에서보세요 : 사용자 정의 JTree에서 비슷한 것을 구현해야합니다. 이미 가지고있는 [SwingX 프로젝트] (http://swingx.java.net)의 JXTree를 사용하십시오 : – kleopatra
은 아마도 JPopup – mKorbel