Reputation: 19822
Scenario is like this: user edits a cell, and presses TAB. Editing is stopped, and value is in the model. However, if postUpdateTestFailed()
returns true, I want to move selection back to the just edited cell so user can do some modification. Problem is, it does not work. It works for anything else but the selected cell in my case... :( Because user presses TAB, focused/selected cell is always the next one. Seems like my selection changing is ignored in this particular case. What am I doing wrong? :)
public void tableChanged(TableModelEvent tme) {
int row = tme.getFirstRow();
int col = tme.getColumn();
int type = tme.getType();
switch (type) {
case TableModelEvent.UPDATE:
currentRow = areenTable.convertRowIndexToView(row);
currentColumn = areenTable.convertColumnIndexToView(col);
if (postUpdateTestFailed()) {
// if the test fails, I want to move back to the edited cell
areenTable.changeSelection(currentRow, currentColumn, false, false);
}
break;
// the rest of switch
} // switch
} // tableChanged() method
Edit 1 : I was hoping that someone experienced the same problem before and solved it somehow...
Edit 2: I would normally do this in my own cell-editor implementation, and prevent commit if validation fails. Unfortunately, in this special case I have to call postUpdateTestFailed()
after the change is committed... And then position cursor to the previously edited cell. Weird thing is that I can move to any other cell, but the last edited one!! I consider this a bug.
Upvotes: 0
Views: 934
Reputation: 51536
couldn't resist to try it (sounded just soo weird enough - and yeah, even me has to try it out and see what's happening, so next time ... ;-)
The problem is that your listener is notified before the table, that is the internal updates are not yet ready. To work, make sure that it's processed after the table's internals, by wrapping into invokeLater, something like
@Override
public void tableChanged(TableModelEvent tme) {
int row = tme.getFirstRow();
int col = tme.getColumn();
int type = tme.getType();
switch (type) {
case TableModelEvent.UPDATE:
final int currentRow = table.convertRowIndexToView(row);
final int currentColumn = table.convertColumnIndexToView(col);
if (currentRow == 0) {
// if the test fails, I want to move back to the edited cell
SwingUtilities.invokeLater(new Runnable() {
public void run() {
table.changeSelection(currentRow, currentColumn, false, false);
}
});
}
break;
// the rest of switch
} // switch
} // tableChanged() method
BTW, I wouldn't listen for changes on the TableModel (they could happen programatically or for other reasons like changes in an underlying data model), instead listen for changes on the table's editor or editingRow/Column property
Upvotes: 2