Making a JTable cell editable - but *not* by double clicking
Solution 1
You will have to make your own cellEditor and ovveride
public boolean isCellEditable( EventObject e )
You can distinguish between single and double click with the clickCount on the eventObject
If its a single Click and its on a selected cell you can return true otherwise return false;
retrieve row and column with
int row = ( (JTable) e.getSource() ).rowAtPoint(e.getPoint());
int column = ( (JTable) e.getSource() ).columnAtPoint(e.getPoint());
to enable F2 you can add custom inputMap en actionMap entries
similar too
table.getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "doMyArrowDown");
table.getTable().getActionMap().put("doMyArrowDown", new ArrowDownAction());
and from your action you can then fire the cellediting yourself
table.editCellAt(row, column );
Solution 2
The DefaultCellEditor has a setClickCountToStart() method to control mouse clicks for editing. The default is 2. Changing this will have no effect on F2 functionality.
Therefore you can set editing to be a triple click.
Not sure exactly how to handle two single clicks to start editing but I guess you would use a Timer to keep track of the first click and then do the editing if the second single click is within you time interval.
Solution 3
I have solved this by wrapping the existing CellEditor
with a Proxy
and intercepting calls to isCellEditable
, returning false for all mouse events and delegating all other calls to the original CellEditor
.
This is slightly more complex than camickr's solution but works for all editors (I have 4 in all.)
Saurabh
Updated on June 15, 2022Comments
-
Saurabh about 2 years
I am trying to add a column to a
JTable
with the following behaviour (similar to Windows Explorer and similar shells):- The cell can be clicked once to select it, as usual.
- The cell can be double-clicked to perform a separate action (launching an external program.)
- The cell value (a string) can still be edited, by single-clicking a second time (after a pause) or by pressing F2 when the cell is highlighted.
Double-clicking must not trigger editing of the cell, but I would like to leave any other default editing triggers operational if possible.
I have tried adding a
MouseListener
to the table, and consuming allMouseEvent
s, but this does not work - if I returntrue
fromisCellEditable()
then my listener never receives any click events but if I returnfalse
then F2 no longer works.Can this be achieved using only event listeners? I would prefer not to mess with the PLAF functions if I can avoid it.
-
Saurabh almost 15 yearsNo need for a timer - it just needs to be too slow to register as a double click.
-
camickr almost 15 yearsI'd be interested in seeing the solution. Also, I don't understand how you distinguish between the initial click on the cell and the click to initiate the editing. Does your code just track the last cell that was selected and when you get two single clicks on the same cell you start editing? If so then the second click could be second or minutes later. Your initial requirement said after a "pause" which is why I suggested a Timer to control the pause interval.
-
Saurabh almost 15 yearsThis is similar to what I did with the proxy. I only override isCellEditable. The key stroke handler is included in the default editor. I do not need to re-implement that.
-
Saurabh almost 15 years@carnickr, I have not implemented that part yet but yes that sounds right. There is no time limit.