Removing a String Editable from JTable

I have a JTable and next to it is a button that calls deleteSelectedRows() , which does exactly what it sounds:

 public void deleteSelectedRows() { int[] selected = jTable.getSelectedRows(); for(int i = selected.length - 1; i >= 0; i--) { model.removeRow(selected[i]); } if(model.getRowCount() < 1) { addEmptyRow(); } } 

But if the cell was edited when it (and / or the cells above it) were deleted, the edited cell remained, and the rest remained, for example:

And then, trying to get out of editing, threw an ArrayIndexOutOfBoundsException , because row 5 tried to be accessible to it, and only one row remained in the table.

Then I tried all kinds of fun and games using jTable.getEditingRow() . At first, adding if(selected[i] != editing) before the deletion seemed to work, but then deleting the rows above the edited cell caused problems.

Then I tried this:

 public void deleteSelectedRows() { int[] selected = jTable.getSelectedRows(); int editing = jTable.getEditingRow(); for(int s : selected) { //PS: Is there a better way of doing a linear search? if(s == editing) { return; } } for(int i = selected.length - 1; i >= 0; i--) { model.removeRow(selected[i]); } if(model.getRowCount() < 1) { addEmptyRow(); } } 

But it doesnโ€™t delete anything, never. Judging by println , I sprinkled, the last cell to be selected (which has a special border marked here on spam ) is considered part of the edit line and thus triggers my early return .

So I donโ€™t care if the solution is related to fixing the original problem - due to awkward results, when the deleted cell is deleted - or this new problem - the getEditingRow() problem does not work as I expected, I just need at least one of them. However, I would be interested to hear both decisions only out of academic curiosity. Thanks in advance.

+4
source share
3 answers

Try including the following lines before deleting any lines from your model:

 if (table.isEditing()) { table.getCellEditor().stopCellEditing(); } 
+6
source

Editing stop tables has a general solution, which is convenient when you have any number of buttons for support.

+1
source

As stated by Howard, before modifying the model, you must stop editing the cell. But it is also necessary to check whether the cell is really modified to exclude null pointer exceptions. This is because the getCellEditor () method returns null if the table is not being edited at the moment:

 if (myTable.isEditing()) // Only if it is being edited myTable.getCellEditor().stopCellEditing(); ... 

There are times when the cell editor may refuse to stop editing, this can happen, for example, if you use some kind of complex editor that is waiting for user input in the dialog box. In this case, you should add an additional check:

 if (myTable.isEditing()) if (!myTable.getCellEditor().stopCellEditing()) { // If your update is user-generated: JOptionPane.showMessageDialog(null, "Please complete cell edition first."); // Either way return without doing the update. return; } 

In your code, you are trying to delete only those lines that are not editable, but this will also throw an ArrayOutOfBounds exception if the cell editor stops editing. It is best to stop it before the update.

Finally, there seems to be a property that you can set in your table:

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

as described here .

0
source

All Articles