Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a JTable cell editable - but *not* by double clicking

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 all MouseEvents, but this does not work - if I return true from isCellEditable() then my listener never receives any click events but if I return false 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.

like image 500
finnw Avatar asked Aug 12 '09 19:08

finnw


People also ask

Is cell editable JTable?

The isCellEditable() method of JTable (or the TableModel) controls whether a cell is editable or not. By default it just return "true". So you can override the method to return a boolean value that is set by your "Modify" button.


2 Answers

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.

like image 57
camickr Avatar answered Oct 01 '22 21:10

camickr


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 );
like image 26
Peter Avatar answered Oct 01 '22 19:10

Peter