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?
Asked
Active
Viewed 5,541 times
2

trashgod
- 203,806
- 29
- 246
- 1,045

Jeffrey Saler
- 21
- 1
- 2
-
1hmm ... you might re-consider the usability of a single click: that's the trigger to un/selection – kleopatra Mar 26 '13 at 13:08
2 Answers
6
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.
-
Only possible by learning from [top contributors like you!](http://stackoverflow.com/tags/swing/topusers). – trashgod Mar 26 '13 at 12:27
-
2>100k (congrats, of course :-) or not: this is not an answer to the question
and jtree already _has_ a binding (to F2 if I remember correctly) – kleopatra Mar 26 '13 at 12:40 -
@kleopatra: Right about `F2` on most platforms, but not `AquaLookAndFeel`. – trashgod Mar 26 '13 at 17:30
-
... so it has no binding to start edit at all? Interesting, item to insert into local knowledgebase :-) – kleopatra Mar 27 '13 at 08:26
-
@kleopatra: Yes. As far as I can tell, `JTree` for `AquaLookAndFeel` has only the two mouse click gestures mentioned in the question. Would invoking `startEditingAtPath()` in a `MouseListener` be an alternative? – trashgod Mar 27 '13 at 11:08
-
curious: why looking for an alternative - doesn's my answer work on mac? – kleopatra Mar 27 '13 at 11:16
-
-
2adding a kludge couldn't really improve it :-) see @mKorbel's comment on my answer: the referenced link does the startEditingPath (though in a selectionListener), it is possible but by-passes api designed to get it working - which is not the best of ideas, IMO – kleopatra Mar 27 '13 at 11:25
4
Technically, you can subclass DefaultTreeCellEditor and tweaks its logic to start editing on the first single click:
JTree tree = new JTree();
tree.setEditable(true);
TreeCellEditor editor =
new DefaultTreeCellEditor(tree, (DefaultTreeCellRenderer) tree.getCellRenderer()) {
@Override
protected boolean canEditImmediately(EventObject event) {
if((event instanceof MouseEvent) &&
SwingUtilities.isLeftMouseButton((MouseEvent)event)) {
MouseEvent me = (MouseEvent)event;
return ((me.getClickCount() >= 1) &&
inHitRegion(me.getX(), me.getY()));
}
return (event == null);
}
};
tree.setCellEditor(editor);
There's a usability quirk, though, as now you can't select without starting an edit - which may or may not be your intention.

kleopatra
- 51,061
- 28
- 99
- 211
-
maybe not [see post by @Michael Dunn](http://www.coderanch.com/t/584508/GUI/java/change-DefaultCellEditor-setClickCountToStart-int-DefaultTreeCellEditor) – mKorbel Mar 26 '13 at 13:18
-
1@mKorbel I'm a bit weary of by-passing the editor api and manually starting the edit. Anyway, the row _is selected_ only it feels a bit unusual to not being able to select without editing .. – kleopatra Mar 26 '13 at 13:27
-
question is like it is, I'm doubt that your good sugestion, with clear concept will be accepted ... – mKorbel Mar 26 '13 at 13:47