问题
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?
回答1:
Assuming you are using a DefaultTableModel, then you just do:
model.setRowCount(0);
回答2:
In order to remove a row from a JTable, you need to remove the target row from the underlying TableModel. If, for instance, your TableModel is an instance of DefaultTableModel, you can remove a row by doing the following:
((DefaultTableModel)myJTable.getModel()).removeRow(rowToRemove);
Updation 2
To know the rows and delete from jtable
int rows = myJTable.getRowCount();
for(int i=0;i<rows;i++)
((DefaultTableModel)myJTable.getModel()).removeRow(i);
来源:https://stackoverflow.com/questions/15053855/how-to-reset-a-jtable