禁用JTree节点的3单击“编辑",但保留键盘键以进行编辑,以在编辑之前保存该节点的旧名称 [英] Disabling 3-Click Edit of a JTree node but keeping keyoard keys to edit for saving the old name of the node prior to edit
问题描述
我希望只能使用 F2 和 ENTER 键编辑节点名称,而不是使用鼠标.我添加了这两行,它们正在工作:
I want to be able to edit the nodename with F2 and ENTER keys only, not with mouse. I added these 2 lines and they are working:
jTree1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "startEditing");
jTree1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "startEditing");
但是如何禁用鼠标编辑?是否有类似以下的方法:jTree1.setToggleClickCount(2);
But how do I disable editing from mouse? Are there any methods like: jTree1.setToggleClickCount(2);
我要这样做的原因是我想保留节点的旧名称,所以我将创建一个侦听器来侦听F2和ENTER并以这种方式保留名称.那有意义吗?有什么想法吗?
The reason I want to do this is that I want to keep the old name of the node, so I'll create a keylistener to listen for F2 and ENTER and keep the names that way. Does that make sense? Any thoughts?
推荐答案
您可以在TreeCellEditor
的帮助下用鼠标禁用编辑,请尝试下一个代码:
You can disable editing with mouse with help of TreeCellEditor
, try next code:
DefaultTreeCellEditor editor = new DefaultTreeCellEditor(t, (DefaultTreeCellRenderer) t.getCellRenderer()){
@Override
public boolean isCellEditable(EventObject event) {
if(event instanceof MouseEvent){
return false;
}
return super.isCellEditable(event);
}
};
该编辑器禁止使用MouseEvent
进行编辑.
that editor prevents editing with MouseEvent
.
将该编辑器设置为您的JTree
,下一行:tree.setCellEditor(editor);
Set that editor to your JTree
with next line: tree.setCellEditor(editor);
其中tree
是您的JTree
.
这篇关于禁用JTree节点的3单击“编辑",但保留键盘键以进行编辑,以在编辑之前保存该节点的旧名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!