问题
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");
But how do I disable editing from mouse? Are there any methods like: jTree1.setToggleClickCount(2);
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?
回答1:
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);
}
};
that editor prevents editing with MouseEvent
.
Set that editor to your JTree
with next line: tree.setCellEditor(editor);
where tree
is your JTree
.
来源:https://stackoverflow.com/questions/21138451/disabling-3-click-edit-of-a-jtree-node-but-keeping-keyoard-keys-to-edit-for-savi