how to clear JTable

前端 未结 7 733
渐次进展
渐次进展 2020-12-08 09:17

How can i clear the content of the JTable using Java..

7条回答
  •  盖世英雄少女心
    2020-12-08 10:04

    Basically, it depends on the TableModel that you are using for your JTable. If you are using the DefaultTableModel then you can do it in two ways:

    DefaultTableModel dm = (DefaultTableModel)table.getModel();
    dm.getDataVector().removeAllElements();
    dm.fireTableDataChanged(); // notifies the JTable that the model has changed
    

    or

    DefaultTableModel dm = (DefaultTableModel)table.getModel();
    while(dm.getRowCount() > 0)
    {
        dm.removeRow(0);
    }
    

    See the JavaDoc of DefaultTableModel for more details

提交回复
热议问题