0

In my program There is a JTable and 2 Buttons named "Save" and "Refresh". My program overview :

Let, Manually i have entered 10 rows into the JTable. Than i updated these data into database by clicking "Save" button.

Now my point is :

When "Refresh" button will be clicked, All the rows of the JTable should be cleared for next entry.

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;
        }
      };

create table with abstract datamodel.

    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);

Add this panel to ContentPane.

   panel.add(pane);
    panel.setBounds(100, 100, 800, 360);
    c.add(panel);

Frame hase a button "Refresh". actionPerformed(ActionEvent e) {} Manually i have entered the data into JTable. Than i updated these data into database by clicking "Save" button.

Now my point is :

When "Refresh" button will be clicked, All the cell of the table should be cleared.

i have implemented TableModeListner as below.

public void tableChanged(TableModelEvent e) {
// TODO Auto-generated method stub
int row = e.getFirstRow();
int column = e.getColumn();

row_sl is intialised with 0.

 if(row_sl >= row){
 if(column == 1){
 String dat = (String) dataModel.getValueAt(row, column);
dataModel.removeTableModelListener(this);

//it fetches database and retribes material name and unit. then set it to respective column.

   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) && (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 && 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<=count;i++){
            for(int j=0;j<=7;j++){
                    dataModel.setValueAt(null, i, j);
            }
        }
        dataModel.addTableModelListener(this);
 }
}

When "Refresh" button clicked the method dataModel.setValueAt(null, i, j); is being called. This method triggers TableModeListner event public void tableChanged(TableModelEvent e).

Hence problem arrises. My requirment is :

When "Refresh button is clicked, all the table cell should be cleared, without affecting my code under this method public void tableChanged(TableModelEvent e){

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.

Arpan
  • 596
  • 2
  • 10
  • 29
Praful
  • 43
  • 2
  • 6
  • unrelated: a) don't do any manual sizing/locating of components, ever b) never ever tweak a component's sizing hints by [setXXSize](http://stackoverflow.com/a/7229519/203657) Instead, use a suitable LayoutManager – kleopatra Feb 08 '13 at 10:35
  • Not sure what you mean by "clear". If you mean remove all the rows of data from the model then you can just use the DefaultTableModel. To clear the model you can use the setRowCount(0) method. – camickr Feb 08 '13 at 16:36

2 Answers2

2

As discussed here, removal requires updating your model's internal data structure. One expedient approach is to simply create the array for some number of rows and columns:

data = new Double[rows][cols];
fireTableDataChanged();

Also consider Arrays.fill() to supplant the default value, null.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0
public static void removeAllDataFromJTable(JTable table) {
    f=flase;
    int numberOfRows = table.getRowCount();
    int numberOfColumns = table.getColumnCount();
    int rowCounter = 0;
    int columnCounter = 0;
    //we will travel to every row and clean it
    for (rowCounter = 0; rowCounter < numberOfRows; rowCounter++) {
        for (columnCounter = 0; columnCounter < numberOfColumns; columnCounter++) {
            table.setValueAt(null, rowCounter, columnCounter);
        }
    }
}

static boolean f = true;

 public void tableChanged(TableModelEvent e) {

 if(f){------}
 }

  public void methode(){
removeAllDataFromJTable(table);
f=true;
  }
mustaphahawi
  • 297
  • 1
  • 8
  • 1
    I'm not sure this quite what the OP wants. Rather then removing the rows, it is simply nulling the values, which may not be appropriate for every cell... – MadProgrammer Feb 08 '13 at 10:37
  • 1
    Also needs to fire a suitable `TableModelEvent`. – trashgod Feb 08 '13 at 10:42
  • Means i have to clear the JTable explicitly. is there any predefined methods ? – Praful Feb 08 '13 at 15:25
  • @trashgod, I'm not sure why you need to fire a TableModelEvent when you are using the setValueAt(...) method. The table setValueAt() method will invoke the model setValueAt() method which in turn will invoke fireTableCellUpdated. – camickr Feb 08 '13 at 16:35
  • 1
    @camickr: Oops, you're right; thanks for commenting. That may expose a limitation of this approach: it will fire `numberOfRows * numberOfColumns` events. – trashgod Feb 08 '13 at 17:57
  • is it a good practice? By calling the frame itself when "Refresh" button clicked and disposing the last frame.. – Praful Feb 11 '13 at 09:43