开发者

Deleting all the rows in a JTable

开发者 https://www.devze.com 2023-03-10 12:38 出处:网络
I need to remove all the rows in my JTable. I have tried both of the following: /** * Removes all the rows in the table

I need to remove all the rows in my JTable.

I have tried both of the following:

/**
 * Removes all the rows in the table
 */
public void clearTable()
{
    DefaultTableModel dm = (DefaultTableModel) getModel();
    dm.getDataVector().removeAllElements();
    revalidate();
}

and

((DefaultTableModel)table.getModel()).setNumRows(0);

N开发者_如何学JAVAeither of which would remove all the rows. Any ideas?


We can use DefaultTableModel.setRowCount(int) for this purpose, refering to Java's Documentation:

public void setRowCount(int rowCount)

Sets the number of rows in the model. If the new size is greater than the current size, new rows are added to the end of the model If the new size is less than the current size, all rows at index rowCount and greater are discarded.

This means, we can clear a table like this:

DefaultTableModel dtm = (DefaultTableModel) jtMyTable.getModel();
dtm.setRowCount(0);

Now, on "how does java discard those rows?", I believe it just calls some C-like free(void*) ultimately somewhen, or maybe it just removes all references to that memory zone and leaves it for GC to care about, the documentation isn't quite clear regarding how this function works internally.


The following code worked for me:

DefaultTableModel dm = (DefaultTableModel) getModel();
int rowCount = dm.getRowCount();
//Remove rows one by one from the end of the table
for (int i = rowCount - 1; i >= 0; i--) {
    dm.removeRow(i);
}


Something like this should work

DefaultTableModel model = (DefaultTableModel)this.getModel(); 
int rows = model.getRowCount(); 
for(int i = rows - 1; i >=0; i--)
{
   model.removeRow(i); 
}


Read the API for DefaultTableModel - setRowCount method supports deleting/discarding all rows in one go...

((DefaultTableModel)myTable.getModel()).setRowCount(0);


Well, setNumRows(0) should work, although if you actually read the API it tells you that this method is obsolete and tell you which method to use instead.

If the code doesn't work, then you are doing something else wrong and we can't tell from the posted code what that might be.

Post your SSCCE that demonstrates the problem.


The simplest way to remove all rows from JTable, just use this method instead...

tablemodel.getDataVector().removeAllElements();
tablemodel.fireTableDataChanged();

tablemodel is the model which you created for your table to add new rows. This is the shortest and fastest way of deleting all rows because what if you have thousands of rows? Looping?


try{

    DefaultTableModel dtm = (DefaultTableModel) jTable2.getModel();

    dtm.setNumRows(0); 

}catch(Exception e){
}


Or if you have lots of rows but very few columns...

DefaultTableModel dtm = new DefaultTableModel();
for(int i=0;i<NUM_COLS;i++) dtm.addColumn(COLUMN_NAME[i]);
myTable.setModel(dtm);

...replaces the old DTM with a fresh one.


This worked for me, just use

tableModel.setRowCount(0); or tableModel.setNumRows(0);

Sets the number of rows in the model. If the new size is greater than the current size, new rows are added to the end of the model If the new size is less than the current size, all rows at index rowCount and greater are discarded. Params: rowCount – number of rows in the model

By setting the number of rows to zero you're are telling the table moble to have no rows, which makes the table empty.


Try this code. This will remove all the rows from JTable.

DefaultTableModel model=new DefaulTableModel(rows,cols);
JTable table=new JTable(model);
for(int i=0;i<model.getRowCount();i=i+0)
{
 model.removeRow(0);
 revalidate();
 }


I had multiple tables, so I created a method to clear "any" table:

private void deleteAllTableRows(JTable table) {
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    while( model.getRowCount() > 0 ){
        model.removeRow(0);
    }
}


DefaultTableModel tm = (DefaultTableModel) tbl.getModel();
while(tbl.getRowCount() > 0)
{
    ((DefaultTableModel) tbl.getModel()).removeRow(0);
}


MyModel myTableModel = (MyModel) myTable.getModel();
for (int i = myTableModel.getRowCount()-1; i >= 0; i--) myTableModel.deleteRow(i);


You can not simple use for loops here because in every loop you remove a row which decreases the row count. Here's the right one:

int count = tmodel.getRowCount();
    while(count > 0) {
        tmodel.removeRow(count-1);
        count = tmodel.getRowCount();
    }


    DefaultTableModel tb = (DefaultTableModel) jTable1.getModel();
    if (jTable1.getSelectedRow() == 0) {
    } else {
        jTable1.selectAll();
        int t = jTable1.getSelectedRow();
        while (t >= 0) {
            tb.removeRow(t);
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号