remove Selected JTable Rows - Java Swing

Java examples for Swing:JTable Row

Description

remove Selected JTable Rows

Demo Code


//package com.java2s;
import javax.swing.*;

import javax.swing.table.DefaultTableModel;

public class Main {
    public static void removeSelectedRows(JTable table) {
        int selections[] = table.getSelectedRows();
        if (selections.length > 0)
            try {
                int lastIndex = selections[0];
                for (int i = selections.length; i > 0; i--) {
                    ((DefaultTableModel) table.getModel()).removeRow(table
                            .getSelectedRow());
                }//from   w  w w.j  av a 2s .c om
                if (table.getRowCount() > 0) {
                    table.setRowSelectionInterval(lastIndex - 1,
                            lastIndex - 1);
                }
            } catch (Exception e1) {
                if (table.getRowCount() > 0)
                    table.setRowSelectionInterval(0, 0);
            }
    }
}

Related Tutorials