I know that you can call this method to delete a row from a JTable:

DefaultTableModel.deleteRow(index);

I need to delete all of the rows at some point, and this is not working:

for (int i=0; i<dtm.getRowCount(); i++)
			 {
				 dtm.removeRow(i);
				 mortgageTable.revalidate();
			 }

I tried revalidating, but that didn't work. This only deletes the last row. Now, at runtime the table consists of only one row and later many rows are added. So I'm thinking the DefaultTableModel is not picking up the newly added rows.

Recommended Answers

All 13 Replies

Think of what you're doing and you get the idea quickly enough.
You delete row 0, then increase i to 1, then move to the next row (which is the old row 2).
Then you delete row 1, increase i to 2, move to the next row.

If you want to do something like this, start at the end and delete from there to the front.
Something like

int numRows == dtm.getRowCount();
for (int i=numRows;i>=0;i++) {
  dtm.removeElement(i);
  mortgageTable.revalidate();
}

should give you the results you are looking for.

WOW! I would have NEVER spoted that. I was thinking since it ran at runtime with only one row that it never updated the count. Thank you so much; I was banging my head against the wall on this one...I wish I could give you a big hug for that. :lol:

I've banged my head against the wall more than once over the same problem, but finally the solution stuck in my head :)

Hi,

Hope this code is simple enough:

while (mortgageTable.getRowCount() > 0) {
	((DefaultTableModel) mortgageTable.getModel()).removeRow(0);
}

Original post was three years ago. I doubt they still need help. Read the dates before posting.

Hi' thanks for reminding the date.

For me, knowledge can never be rotten. Each day there will be someone seeking better way to programs. Maybe they stumble daniweb and appreciate what we share here despite how old the codes is :) .

Original post was three years ago. I doubt they still need help. Read the dates before posting.

public synchronized void clearTable(){
    int numrows = tblSales.getRowCount(); 
    for(int i = numrows - 1; i >=0; i--){
        modelTblsales.removeRow(i);
    }
}

This is more efficient way:

//deletes ALL the rows
DefaultTableModel.getDataVector().removeAllElements();
//repaints the table and notify all listeners (only once!)
DefaultTableModel.fireTableDataChanged();

If you do this

while (mortgageTable.getRowCount() > 0) {
	((DefaultTableModel) mortgageTable.getModel()).removeRow(0);
}

This will fire notificantions to all the listeners everytime you delete one row, so, if you have 100000 rows........

This is more efficient way:

//deletes ALL the rows
DefaultTableModel.getDataVector().removeAllElements();
//repaints the table and notify all listeners (only once!)
DefaultTableModel.fireTableDataChanged();

If you do this

while (mortgageTable.getRowCount() > 0) {
	((DefaultTableModel) mortgageTable.getModel()).removeRow(0);
}

This will fire notificantions to all the listeners everytime you delete one row, so, if you have 100000 rows........

let me guess ... your clock stopped in the past too?

HI!!!
I Just want to thank muhdazwa and gregsci! You both help soooo much.
I've been searching everywhere and nothing seem to work, till i found this post!

Thank You! :D

import javax.swing.*;
import javax.swing.table.*;

public class RemoveRows{
  public static void main(String[] args) {
  new RemoveRows();
  }

  public RemoveRows(){
  JFrame frame = new JFrame("Inserting rows in the table!");
  JPanel panel = new JPanel();
  String data[][] = {{"Vinod","100"},{"Raju","200"},{"Ranju","300"},
    {"Rahul","400"},{"Noor","600"}};
  String col[] = {"Name","code"};
  DefaultTableModel model = new DefaultTableModel(data,col);
  JTable table = new JTable(model);
  System.out.println("Befoure removing, number of rows: " 
    + model.getRowCount());
  //Remove first row
  model.removeRow(0);
  System.out.println("After removing first row, number of rows: " 
    + model.getRowCount());
  //Remove lase row
  model.removeRow(table.getRowCount()-1);
  System.out.println("After removing last row, number of rows: " 
   + table.getRowCount());
  panel.add(table);
  frame.add(panel);
  frame.setSize(300,300);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

Simpi: this thread's "life" ended about 7 years ago.

let the deceased rest in peace. there are enough still active threads where you can help people, there's no need to revive old ones.

this post helped me too , thanks everyone .
Let this post live forever !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.