how to clear JTable

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

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

相关标签:
7条回答
  • 2020-12-08 09:57

    If we use tMOdel.setRowCount(0); we can get Empty table.

    DefaultTableModel tMOdel = (DefaultTableModel) jtableName.getModel();
    tMOdel.setRowCount(0);
    
    0 讨论(0)
  • 2020-12-08 09:59

    You must remove the data from the TableModel used for the table.

    If using the DefaultTableModel, just set the row count to zero. This will delete the rows and fire the TableModelEvent to update the GUI.

    JTable table;
    …
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    model.setRowCount(0);

    If you are using other TableModel, please check the documentation.

    0 讨论(0)
  • 2020-12-08 10:02

    I had to get clean table without columns. I have done folowing:

    jMyTable.setModel(new DefaultTableModel());
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-08 10:17

    I think you meant that you want to clear all the cells in the jTable and make it just like a new blank jTable. For an example, if your table is myTable, you can do following.

    DefaultTableModel model = new DefaultTableModel();
    myTable.setModel(model);
    
    0 讨论(0)
  • 2020-12-08 10:18

    This is the fastest and easiest way that I have found;

    while (tableModel.getRowCount()>0)
              {
                 tableModel.removeRow(0);
              }
    

    This clears the table lickety split and leaves it ready for new data.

    0 讨论(0)
提交回复
热议问题