How do you remove selected rows from a JTable?

前端 未结 18 1169
情话喂你
情话喂你 2020-12-30 03:26

I\'ve tried this:

public void removeSelectedFromTable(JTable from)
{
    int[] rows = from.getSelectedRows();
    TableModel tm= from.getModel();

    while(         


        
18条回答
  •  礼貌的吻别
    2020-12-30 04:10

    The stream solution above doesn't take into account sorting, it may be fixed this way:

            IntStream.of(table.getSelectedRows())
                .boxed().map(i -> table.convertRowIndexToModel(i))
                .sorted(Collections.reverseOrder())
                .forEach(((DefaultTableModel)table.getModel())::removeRow);
    

提交回复
热议问题