5

i've got an issue with Jtable and my dataModel. My table model extends AbstracttableModel, the datas are stored in a Vector. I'have a function witch is suppose to remove one or more row. These rows are not necessarily contiguous because my jtable set a selectionMode as this:

jTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

The function to remove row (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 launch the function, i execute the loop without problem, in step-by-step mode, i can see the vData object updated and if i execute this only one time there is no probleme with the GUI. The problem appear when i select more than one row. For example, i selected row number 0 and number 1 in my table and i lauch the function removeMessageRow, at the first execution, the vDataObject is correctly updated (all the datas are shiffted and the last elements of this vector is set to null by the call to vData.remove(m). So in my case, i expect that at the second execution, the object to find should be at position 0 but it's at position 1 as the vData Object as never been updated. Does anybody have an idea about that? I've tried many on fire... stuff but no one semms to execute immediatly. Thanks for any help by advance and sorry for my Shakespeare's language.

Simon Mardiné
  • 510
  • 2
  • 7
  • 23

2 Answers2

18

Add a method in your model taking a collection of indices to remove (Set<Integer>, or int[]), sort this collection, iterate backwards in this collection, and remove the elements 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]);
    }
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Hi, I just came across the same problem. This solution indeed works. Can you please explain why you need to iterate the collection in reverse order? Thank you. :) – Mihai Sep 18 '13 at 07:24
  • 2
    Because if you want to remove elements at indices 1 and 4, as soon as you have removed the element at index 1, the element that was at index 4 is now at index 3. – JB Nizet Sep 18 '13 at 16:44
  • Thank you for concise solution. To ensure that duplicate indices can not be exist in the input, I would remove duplicates from the input array or pass indices as Set. – mmdemirbas Oct 25 '13 at 11:04
0
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
  • Welcome to StackOverFlow, please provide an explanation also to support your code snippet, so other users can learn from their mistakes and will help the community. – Nomesh DeSilva Aug 23 '15 at 07:54