0
votes

I have some old Java application that uses JTable to show some data and allow input as well. JTable has 2 columns. Next to JTable, there is button that adds new row into JTable.

My problem is behavior of Tab and Enter keyboard keys and mouse clicks when you navigate JTable.

I will try to explain: Let say there are 5 rows in JTable:

  1. if I click with mouse on one row, sometime whole row gets highlighted, and sometime cell gets into edit mode (in addition to whole row being higlighted)
  2. Sometime, selected row gets highlighted, but cell above or below highlighted row gets into edit mode
  3. If I use Tab to skip from one field to another, editable field is always above highlighted row.

There are other issues as well.

Any ideas what might be wrong with it?

2
this question is hot adept for to edit the question with a SSCCE - mKorbel

2 Answers

1
votes

That sounds a bit odd. Are you fully invalidating your table and notifying listeners etc that the number of rows have changed?

In your table model, loop over all of your TableModelListeners and fire a TableModelEvent 'insert' event:

TableModelEvent event = new TableModelEvent(
                modelInstance,
                positionOfNewRow,
                positionOfNewRow,
                TableModelEvent.ALL_COLUMNS,
                TableModelEvent.INSERT);

for (TableModelListener l: listeners) {
    l.tableChanged(event);
}

If you can't work out the problem, it is possible to write your own behaviour for the table.

These are some useful methods:

editCellAt(int row, int column);
rowAtPoint(Point point);
columnAtPoint(Point point);

You can add your own listeners to the table to intercept events and edit cells in any way you like.

0
votes

Turn off the Table cell editing. And see how it behaves. Typically this can be handled by changing the TableModel.isCellEditable() method to simply return false.

Turn it back on. Is your instance of JTable subclassed? Is it overriding editCell()? If so that's the method that will trigger an edit based on the event occurring or not. That method is turning on editing inappropriately. The implementation of that method is bad if it's overridden.

If your table isn't subclassed look for calls to editCell(). Those are probably calling it inappropriately so you can look for those calls and start setting breakpoints or log statements.