How do you remove selected rows from a JTable?

前端 未结 18 1171
情话喂你
情话喂你 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 03:50

    Try this:

    int selectedIndex = table_.getSelectedRow();
    while (selectedIndex >= 0) {
        int modelIndex = table_.convertRowIndexToModel(selectedIndex);
        tableModel_.removeRow(modelIndex);
    
        selectedIndex = table_.getSelectedRow();
    }
    
    0 讨论(0)
  • 2020-12-30 03:51

    This code work for me!

    DefaultTableModel model = (DefaultTableModel) Table.getModel();
        int nbrrow=Table.getRowCount();
    
            if(nbrrow>=1){
                int[] rows = Table.getSelectedRows();
                if(rows.length>=1){
                if(Table.getModel().isCellEditable(Table.getSelectedRow(),Table.getSelectedColumn())
                           &&Table.isEditing()==true)
                   {
                       Table.getCellEditor().stopCellEditing();
                        for(int i=0;i<rows.length;i++){ 
                         model.removeRow(rows[i]-i);
                       }
                   }else{
                                for(int i=0;i<rows.length;i++){ 
                                 model.removeRow(rows[i]-i);
                               }
    
                    }
                for(int i=0;i<Table.getRowCount();i++){
                               model.setValueAt(i, i, 0);
                           }
                  Table.revalidate();
                  Table.repaint();
                }
            }
    
    0 讨论(0)
  • 2020-12-30 03:54

    Try this

        public void removeSelectedFromTable(JTable table) {
    
            DefaultTableModel model = (DefaultTableModel) table.getModel();
            int indexes[] = table.getSelectedRows(); 
            int res = 0;
            for(int i = 0; i < indexes.length; i++) {
                res += (i>0)?(indexes[i]-indexes[i-1]-1):0;
                int index = table.convertRowIndexToModel(indexes[0]+res)
                model.removeRow(index);
            }
        }
    
    0 讨论(0)
  • 2020-12-30 03:55

    I looked at all the solutions, but there are some exceptions, so I would like to add this answer.

    for (int i = 0; i < raws.length; i++) {
        System.out.println("I : " + i);
        for (int j = 1; j <= 1; j++) {
            defaultTableModel.removeRow(0);
            jTStockJournal.clearSelection();
            defaultTableModel.fireTableDataChanged();
        }
    }
    
    0 讨论(0)
  • 2020-12-30 03:55

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    DefaultTableModel defaultModel = (DefaultTableModel) jTable1.getModel(); Vector newRow = new Vector(); defaultModel.removeRow(jTable1.getSelectedRow()); i = i - 1; }

    0 讨论(0)
  • 2020-12-30 03:56

    That works for me:

    if(tablaNotif.getSelectedRows().length>0){
        int[] rows = tablaNotif.getSelectedRows();
        if(tablaNotif.getSelectedRows().length<2) {
            modelo.remove(tablaNotif.convertRowIndexToModel(rows[0]));
        } else{
            for(int i = 0; i<tablaNotif.getSelectedRows().length;i++) {
                modelo.remove(tablaNotif.convertRowIndexToModel(rows[i]));
            }
            modelo.remove(tablaNotif.convertRowIndexToModel(rows[(tablaNotif.getSelectedRows().length-1)]));
        }
    }
    
    0 讨论(0)
提交回复
热议问题