title: How to clear a JTable contents after pressing a JButton ?
body: <p>In my program There is a JTable and 2 Buttons named "Save" and "Refresh".
  My program overview :</p>
  
  <p>Let, Manually i have entered 10 rows into the JTable.
  Than i updated these data into database by clicking "Save" button.</p>
  
  <p>Now my point is :</p>
  
  <p>When "Refresh" button will be clicked, All the rows of the JTable should be cleared for next entry.</p>
  
  <pre><code>dataModel = new AbstractTableModel() {
          private String[] columnNames = {"Sl No","Material Code", "Material         Name", "Quntity", "Unit", "Unit/Price","Total Price", "Status" };//new String[9];
          private Object[][] data = new Object[100][9];
  
            public int getColumnCount() { return columnNames.length; }
            public int getRowCount() { return data.length;}
            public String getColumnName(int col) {
              // TODO Auto-generated method stub
              return columnNames[col];
          }
            public Object getValueAt(int row, int col) {
              // TODO Auto-generated method stub
              return data[row][col];
          }
  
          @Override
          public void setValueAt(Object aValue, int row, int col) {
              // TODO Auto-generated method stub
              data[row][col] = aValue;
              fireTableCellUpdated(row, col);
          }
  
          public boolean isCellEditable(int rowIndex, int columnIndex) {
              // TODO Auto-generated method stub
              return true;
          }
          public Class getColumnClass(int col) {
              if((col == 3) || (col == 5) || (col == 6))
                  return Double.class;
              //return getValueAt(0, c).getClass();
              return String.class;
          }
        };
  </code></pre>
  
  <p>create table with abstract datamodel.</p>
  
  <pre><code>    table = new JTable(dataModel);
      JTableHeader header = table.getTableHeader();
      header.setBackground(Color.yellow);
      JScrollPane pane = new JScrollPane(table);
      pane.setPreferredSize(new Dimension(800, 350));
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  </code></pre>
  
  <p>Add this panel to ContentPane.</p>
  
  <pre><code>   panel.add(pane);
      panel.setBounds(100, 100, 800, 360);
      c.add(panel);
  </code></pre>
  
  <p>Frame hase a button "Refresh". <code>actionPerformed(ActionEvent e) {}</code>
  Manually i have entered the data into JTable.
  Than i updated these data into database by clicking "Save" button.</p>
  
  <p>Now my point is :</p>
  
  <p>When "Refresh" button will be clicked, All the cell of the table should be cleared.</p>
  
  <p>i have implemented <code>TableModeListner</code> as below.</p>
  
  <pre><code>public void tableChanged(TableModelEvent e) {
  // TODO Auto-generated method stub
  int row = e.getFirstRow();
  int column = e.getColumn();
  </code></pre>
  
  <p>row_sl is intialised with 0.</p>
  
  <pre><code> if(row_sl &gt;= row){
   if(column == 1){
   String dat = (String) dataModel.getValueAt(row, column);
  dataModel.removeTableModelListener(this);
  </code></pre>
  
  <p>//it fetches database and retribes material name and unit. then set it to respective column.</p>
  
  <pre><code>   mat.getmaterial_name(dat); 
         dataModel.setValueAt(mat.materialname, row, column+1);
  dataModel.setValueAt(mat.unit, row, column+3);
  //dataModel.setValueAt(dat, 1, 2);
  dataModel.addTableModelListener(this);
  }
  if((column == 5) &amp;&amp; (Double)dataModel.getValueAt(row, 3) != null){
      dataModel.removeTableModelListener(this);
      Double d = (Double) dataModel.getValueAt(row, 3);
      d = d * (Double)dataModel.getValueAt(row, column);
      dataModel.setValueAt(d, row, 6);
      dataModel.addTableModelListener(this);
  }
  if(row == row_sl &amp;&amp; column == 7)
      row_sl ++;
  }
  else
      new error_frame("Complete the row no:"+row_sl);
  
    public void actionPerformed(ActionEvent e) {
    if(e.getSource() == Refresh){
       dataModel.removeTableModelListener(this);
          for (int i=0;i&lt;=count;i++){
              for(int j=0;j&lt;=7;j++){
                      dataModel.setValueAt(null, i, j);
              }
          }
          dataModel.addTableModelListener(this);
   }
  }
  </code></pre>
  
  <p>When "Refresh" button clicked the method <code>dataModel.setValueAt(null, i, j);</code> is being called. This method triggers <code>TableModeListner</code> event <code>public void tableChanged(TableModelEvent e)</code>.</p>
  
  <p>Hence problem arrises.
  My requirment is :</p>
  
  <p>When "Refresh button is clicked, all the table cell should be cleared, without affecting my code under this method <code>public void tableChanged(TableModelEvent e){</code></p>
  
  <p>Kindly suggest me in this regard.
  I have another option when "Refresh" button is clicked it will call the frame itself by disposing the last on. Is it good practice to do ? Suggest.</p>