How to reset a JTable

11,649

Assuming you are using a DefaultTableModel, then you just do:

model.setRowCount(0);
Share:
11,649
Roshan George
Author by

Roshan George

Updated on June 11, 2022

Comments

  • Roshan George
    Roshan George almost 2 years

    I have a question with respect to JScrollPane and a JTable.

    I have added a JTable to a JScrollPane, and added the JScrollPane to the JPanel. When I click on 'show' button, the JTable will be filled with contents from the database.

    I also have another button reset, clicking on which will remove the contents of the JTable, and the JScrollPane. It is supposed to be doing that, but what happens is that, even after clicking the button, all the contents of the JTable and the JScrollPane still exists.

    I used revalidate(), reinstantiate(), etc, but of no use. How do I make it work?

  • Roshan George
    Roshan George about 11 years
    Since I don't know how many rows are there, I think this won't work.
  • camickr
    camickr about 11 years
    Not 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
    kleopatra about 11 years
    -1 for a) @camickr already noted the incorrect looping b) not using api designed to handle the remove all
  • kleopatra
    kleopatra about 11 years
    correct 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
    Arpan almost 10 years
    I 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.