问题描述
我有一个 JTree
,并希望在我单击时调用其 getTreeCellEditorComponent()
方法在一个节点上。根据 DefaultTreeCellEditor
类(我扩展)的文档,点击三次鼠标,或点击,暂停,点击和延迟1200后开始编辑毫秒。有没有办法覆盖这种行为,以便单击可以开始编辑过程?
I have a JTree
, and would like for its getTreeCellEditorComponent()
method to be invoked when I single click on a node. According to the documentation for the DefaultTreeCellEditor
class (which I extended), "Editing is started on a triple mouse click, or after a click, pause, click and a delay of 1200 miliseconds." Is there some way to override this behavior, so that a single-click could start the editing process?
推荐答案
API建议使用 MouseListener
,但是键绑定也很方便。此示例调用 startEditingAtPath()
并绑定到Enter键:
The JTree
API recommends a MouseListener
, but a key binding is also handy. This example invokes startEditingAtPath()
and binds to the Enter key:
final JTree tree = new JTree();
tree.setEditable(true);
MouseListener ml = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int row = tree.getRowForLocation(e.getX(), e.getY());
TreePath path = tree.getPathForLocation(e.getX(), e.getY());
if (row != -1) {
if (e.getClickCount() == 1) {
tree.startEditingAtPath(path);
}
}
}
};
tree.addMouseListener(ml);
tree.getInputMap().put(
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "startEditing");
附录:另见关于可用性。
Addendum: See also this answer regarding usability.
这篇关于如何通过单击编辑JTree节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!