-1

I want to delete multiple rows at a times on selection.

Here is my code:

            int[] indexList = queryTable.getSelectedRows();
            queryTableModel.removeRows(indexList);
            queryTable.clearSelection();
            SwingUtilities.updateComponentTreeUI(queryTable);

Please help.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user3824693
  • 49
  • 2
  • 8
  • Please show your TableModel inplementation... Why `SwingUtilities.updateComponentTreeUI(queryTable)`? – dic19 Aug 13 '14 at 13:11
  • Don't use SwingUtilities.updateComponentTreeUI(). The is not need to use that method. The TableModel will notify the table when data has changed so the table can repaint itself. – camickr Aug 13 '14 at 20:33

4 Answers4

2

Here is simple example of removing selected rows from model:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TestFrame extends JFrame {

    private DefaultTableModel model;

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        final JTable t = new JTable(model = new DefaultTableModel(0,1));
        for(int i =0;i<10;i++){
            model.addRow(new Object[]{i});
        }
        JButton removeSelected = new JButton("remove");
        removeSelected.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int[] selectedRows = t.getSelectedRows();
                for(int i=selectedRows.length-1;i >= 0;i--){
                    model.removeRow(selectedRows[i]);;
                }
            }
        });
        add(new JScrollPane(t));
        add(removeSelected,BorderLayout.SOUTH);
    }


    public static void main(String args[]) {
        new TestFrame();
    }

}
alex2410
  • 10,904
  • 3
  • 25
  • 41
2

Assuming you are using DefaultTableModel as table model, this should be enough:

int[] viewIndexes = table.table.getSelectedRows();
for(int i = viewIndexes.length - 1; i >= 0; i-- ) {
    int modelIndex = table.convertRowIndexToModel(viewIndexes[i]);
    ((DefaultTableModel)table.getModel()).removeRow(modelIndex);
}

Never forget to convert the selected indexes from view to model. Otherwise you'll have problems if your table is sorted.

If you are using a custom TableModel, the process is almost the same, there won't be great differences.

In addition, you don't have to do nothing to update the view after adding/deleting/updating data, the model will notify the view in such events and this last one will be updated accordingly.

See How to use Tables tutorial for further details.

dic19
  • 17,821
  • 6
  • 40
  • 69
  • 1
    This will not work. When you remove the first row all the other rows are shifted down by one so the indexs of the selected rows is no longer correct. You need to remove the rows from the end of the TableModel. – camickr Aug 13 '14 at 20:36
  • Thanks, the point was about converting view index to its respective model index and I've totally overlooked that @camickr – dic19 Aug 13 '14 at 21:02
2

How to delete multiple rows from beantablemodel in java?

There is no BeanTableModel in Java.

If you happen to be referring to this Bean Table Model then you can use the removeRows(...) method.

camickr
  • 321,443
  • 19
  • 166
  • 288
0
// Selected rows by the user
int selectedRows[] = table.getSelectedRows();

if (selectedRows.length > 0) {
    // Every time a row is removed, the array will fill the empty gap moving 1 position all 
    // items that come after the removed item, so the next position to remove will be 
    // index + 1, then +2, +3, etc...
    int compensation = 0;

    // Removes row from selected index and compensates the misalignment
    for (int row : selectedRows) {
        model.removeRow(row - compensation++);
    }
}
Marc
  • 19
  • 6