1

I have a JXTable where the users need to introduce data, then save it. Only the thing is, the user has to deselect the last edited cell before saving it. If they don't, the data of that cell isn't saved.

The only thing I thought of is to change the current selection automatically just before saving. This is what i tried :

table.getSelectionModel().setSelectionInterval(0, 0);
table.getColumnModel().getSelectionModel().setSelectionInterval(0, 0);

OR

table.getSelectionModel().setLeadSelectionIndex(0);
table.getColumnModel().getSelectionModel().setLeadSelectionIndex(0);

None of both seem to work yet these are the only two methods I found to do this.

Can anyone please tell me how to do this properly or propose an alternative to also let it save the data from that last cell?

J. Steen
  • 15,470
  • 15
  • 56
  • 63
Xaviraan
  • 73
  • 1
  • 5
  • a) is the behaviour different from core jtable? b) exactly how's the save implemented (click a button, menuitem, key binding)? – kleopatra Dec 03 '12 at 16:09
  • Also see [this solution](http://stackoverflow.com/a/3562764/1076463) – Robin Dec 03 '12 at 16:18

1 Answers1

1

I am assuming the user clicks on another component (JButton) when he wishes to save data. If you have a reference to the JXTable when that event happens you could add the following piece of code there:

if (table.isEditing()) {
    table.getCellEditor().stopCellEditing();
}

The stopCellEditing() should save the state of the model and allow you to save all the contents, including the currently selected / edited cell.


EDIT: As kleopatra pointed out, the default (and better!) way to handle this is through the client property of JTable component:

table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

For JXTable this should already be set though, which indicates that the way your UI handling of the save functionality works does not include moving the focus away from the table. So in essence you'd be better off changing the focus when your 'save' event is being fired.

Community
  • 1
  • 1
Hermann Hans
  • 1,798
  • 1
  • 13
  • 24
  • 1
    not always needed: as long as the button is focusable, the thingy to do is set the table's terminatesEditOnFocusLost client property to true. Which is default in jxTable, so probably the button in question isn't focusable - and your solution indeed is an option :-) – kleopatra Dec 03 '12 at 16:12
  • 1
    Thanks, good point. I've edited the answer to point out that the better way would be to properly switch the focus away from the JXTable. – Hermann Hans Dec 03 '12 at 16:23
  • Thank you so much, Herminator! The following code works like a charm! if (table.isEditing()) { table.getCellEditor().stopCellEditing(); } The "terminateEditOnFocusLost" didn't change anything, but thanks for the help! With the code I used, the selection DID move to where I pointed it, but the data in the last edited cell wasn't saved. But with the above mentioned code, this problem is solved. Thanks again! – Xaviraan Dec 04 '12 at 09:44
  • @Xaviraan: beware (and repeating myself ;-) - if this helps, it's worth trying to find out _why_ it is needed. It _should_ work without in most contexts, so there might or not be a bug somewhere (in your code or in the framework code) that's covered (vs. solved) by explicitly terminating. – kleopatra Dec 04 '12 at 15:30