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.