JTable cell editing deselects row, workaround?



Hi,

I've a JTable table set up with single-select mode and only rows selectable.

To summarize the problem, a row should be automatically selected when the user clicks on any cell in that row (works!), however there's one column in the jtable that contains editable checkbox cells, and when the user clicks such a checkbox cell the row selection disappears (i.e. row select does not work! but it should! :-)).

Some jtable init code:

....
private JTable magTable;
private MagneticComponentTableModel magTableModel;
....
magTableModel = new MagneticComponentTableModel();
magTable = new JTable(magTableModel);
magTable.getTableHeader().setReorderingAllowed(false);
magTable.getTableHeader().setResizingAllowed(true);

magTable.setColumnSelectionAllowed(false);
magTable.setRowSelectionAllowed(true);

StyledTableCellRenderer stcrDefault = new StyledTableCellRenderer();
stcrDefault.setTooltipVisible(true);
magTable.setDefaultRenderer(StyledWrapper.class, stcrDefault);
magTable.setDefaultEditor(StyledWrapper.class, new StyledCellEditor(new JTextField()));

magTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

magTableModel.setTable(magTable);
....

When the user clicks a cell that is not editable (isCellEditable() returns false), then the JTable automatically will select the entire corresponding row.

The row selection does not change until the user clicks some cell in a different row. This is the desired behaviour.

But for cells where isCellEditable() has been set to return true, like for example for a column that contains editable checkboxes (getColumnClass()=>Boolean.class), the row will not be selected automatically. Or, well, for text cells the row is selected when the user clicks the cell, but when the user starts to edit the cell and completes editing, then JTable deselects the row. It is worse for the checkbox cells, merely clicking the cell changes the checkbox state (desired), but also deselects the entire row. Very much undesired behaviour! :)

Is there any workaround for this?

Something involving CellEditorListener doesn't work, probably.

Any ideas?

- Jan
.