简体   繁体   中英

How to delete multiple rows from beantablemodel in java?

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.

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();
    }

}

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.

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.

// 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++);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM