How to reset a JTable
11,649
Assuming you are using a DefaultTableModel, then you just do:
model.setRowCount(0);
Author by
Roshan George
Updated on June 11, 2022Comments
-
Roshan George almost 2 years
I have a question with respect to
JScrollPane
and aJTable
.I have added a
JTable
to aJScrollPane
, and added theJScrollPane
to theJPanel
. When I click on 'show' button, theJTable
will be filled with contents from the database.I also have another button
reset
, clicking on which will remove the contents of theJTable
, and theJScrollPane
. It is supposed to be doing that, but what happens is that, even after clicking the button, all the contents of theJTable
and theJScrollPane
still exists.I used
revalidate()
,reinstantiate()
, etc, but of no use. How do I make it work? -
Roshan George about 11 yearsSince I don't know how many rows are there, I think this won't work.
-
camickr about 11 yearsNot sure if this deserves a downvote or not. Yes, the removeRow() is used to delete rows. However, when deleting rows you need to start at the last row and decrement i by 1 until you reach zero, otherwise you will only delete half of the rows.
-
kleopatra about 11 years-1 for a) @camickr already noted the incorrect looping b) not using api designed to handle the remove all
-
kleopatra about 11 yearscorrect answer, given the less than complete question :-) Wildly guessing that the OP might want to reset the structure as well (the snippet reads in a completely new table)
-
Arpan almost 10 yearsI haven't used it but it seems like it will just remove all rows. But the question how to reset it means he wants to remove all data and reload the table.