Delete multiple rows in a single pass in JTable AbstractDataModel

I have a problem with Jtable and my dataModel. My table model extends AbstracttableModel, the data is stored in a vector. I have a function that involves deleting one or more lines. These lines do not have to be contiguous because my jtable set selectionMode as follows:

jTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 

Row deletion function (one by one):

 public void removeMessageRow(Integer p_idMessage) { Enumeration<MlMessage> alldatas = vData.elements(); while (alldatas.hasMoreElements()) { MlMessage m = alldatas.nextElement(); if (m.getIdMessage() == p_idMessage) { int idx = vData.indexOf(m); boolean result = vData.remove(m); // fireTableDataChanged(); // fireTableRowsDeleted(idx, idx); fireTableStructureChanged(); return; } } 

When I run the function, I run the loop without problems, in step-by-step mode I can see that the vData object is updated, and if I execute it only once, there are no problems with the GUI. The problem occurs when I select multiple rows. For example, I selected row number 0 and number 1 in my table, and I executed the removeMessageRow function, the first time vDataObject was updated correctly (all data is turned upside down and the last elements of this vector are set to zero, call vData.remove(m) . Therefore, in In my case, I expect that on the second run, the object that should be found should be in position 0, but in position 1 as a vData object, it has never been updated. Does anyone have an idea about this? I tried a lot on fire .. Something but no one could execute immediately. Thanks for any help in advance and sorry for my Shakespeare language.

+4
source share
2 answers

Add a method to your model, taking the set of indices to delete ( Set<Integer> or int[] ), sort this collection, iterate back into this collection and delete the items in the list of objects of your model:

 public void removeRows(int[] indices) { Arrays.sort(indices); for (int i = indices.length - 1; i >= 0; i--) { this.data.remove(indices[i]); fireTableRowsDeleted(indices[i], indices[i]); } } 
+14
source
 int[] selectedRow = tblUsuarios.getSelectedRows(); int del=1; for(int j=0; j<selectedRow.length; j++){ modTabla.removeRow(selectedRow[j]); if(j<selectedRow.length-1){ selectedRow[j+1] = selectedRow[j+1]-del; del = del+1; } } //modTabla = defaultTableModel //tblUsuarios = jtable 
0
source

Source: https://habr.com/ru/post/1383410/


All Articles