Question

I need to have editable JTree but editing should be started by double clicking on node or presssing F2 instead of just pressing mouse/clicking on node as it is set by default.

What is the best way to disable handling of mouse press or single mouse click on JTree (or any other similar component like JTable for example) but keeping the selection handling?

I have code for starting editing I just need to turn off JTree starting editing with another events.

Was it helpful?

Solution

As I understand, you need to manage editor of JTree, here is simple example:

    JTree t = new JTree();
    t.setEditable(true);
    TreeCellRenderer cellRenderer = t.getCellRenderer();
    DefaultTreeCellEditor cellEditor = new DefaultTreeCellEditor(t, (DefaultTreeCellRenderer) cellRenderer){
        @Override
        public boolean isCellEditable(EventObject arg0) {
            if(arg0 instanceof MouseEvent){
                return ((MouseEvent)arg0).getClickCount() > 2;
            }
            return super.isCellEditable(arg0);
        }
    };
    t.setCellEditor(cellEditor);

examine DefaultTreeCellEditor and it's method isCellEditable(EventObject arg0).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top