Wednesday, July 7, 2010

Customized TableModel

import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class CustomizedTableModel extends AbstractTableModel
{
private String[] columnNames;
Vector cache;
String[] headers;
int colCount;
int dataLength;
private Object[][] data;
Class type[] = null;
public CustomizedTableModel(Class [] coltype, String [] columnNames)
{
type = coltype;
this.columnNames = columnNames;
headers = columnNames;
colCount = coltype.length;
cache = new Vector();

}

public int getColumnCount() {

return colCount;
}

public int getRowCount() {
return dataLength;

}


public String getColumnName(int col) {

return headers[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];

}


public Class getColumnClass(int col) { return type[col]; }



public boolean isCellEditable(int row, int col) {
return false;
}


public void setValueAt(Object value, int row, int col) {

data[row][col] = value;
fireTableCellUpdated(row, col);

}

public void setDataSet(String[][] data)
{
dataLength = data.length;
try {
this.data = new Object[data.length][colCount];
for (int i = 0; i < data.length; i++)
{
for(int j = 0; j < colCount; j++)
{
this.data[i][j] = data[i][j];
}
}
}
catch(Exception e)
{
cache = new Vector();
e.printStackTrace();
}
fireTableChanged(null);

}
}



///calling of it///////////////

Class coltype [] = {String.class,String.class,String.class};
String colNames [] = {"EmpId","First Name","Last Name"};
CustomizedTableModel ctm = new CustomizedTableModel(coltype,colNames);

No comments:

Post a Comment