Create an editable JTable cell - but * not * by double-clicking

I am trying to add a column in JTablewith the following behavior (similar to windows explorer and similar shells):

  • You can click a cell once to select it as usual.
  • You can double-click a cell to perform a separate action (launch an external program.)
  • The cell value (row) can still be edited by clicking a second time (after a pause) or by clicking F2when the cell is highlighted.

A double click should not trigger cell editing, but I would like to leave any other editing triggers to default if possible.

I tried adding to the table MouseListenerand consuming all MouseEvents, but this will not work - if I return truefrom isCellEditable(), then my listener will never receive any click events, but if I return false, then it F2does not work anymore.

Can this be done using only event listeners? I would prefer not to mess with PLAF functions if I can avoid this.

+5
source share
3 answers

You will need to make your own cellEditor and ovveride

public boolean isCellEditable( EventObject e )

You can distinguish between single and double click using clickCount on eventObject

If it is one click away and you can return true in the selected cell, otherwise return false;

int row = ( (JTable) e.getSource() ).rowAtPoint(e.getPoint());
int column = ( (JTable) e.getSource() ).columnAtPoint(e.getPoint());

F2, Map en actionMap

similar too
table.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "doMyArrowDown");
table.getTable().getActionMap().put("doMyArrowDown", new ArrowDownAction()); 

table.editCellAt(row, column );
+3

DefaultCellEditor setClickCountToStart() . - 2. F2.

.

, , , , , , , .

+4

I solved this by wrapping an existing one CellEditor Proxyand intercepting calls isCellEditable, returning false for all mouse events and delegating all other calls to the original CellEditor.

This is a bit more complicated than the camickr solution, but it works for all editors (I only have 4).

+1
source

All Articles