JTable's columns rendering/formatting
Hello Everyone,
I'd like to post and ask for advices regarding the correct way to implement a JTable that has for given columns certain rules.
Let us suppose I want to have a table with a date column and two other columns that accepts only Integers and Doubles respectively. These can be modified and must respect the constraints given. Which is the right way to do this?
Moreover I have implemented a TableModel that stores data in a Vector<MyObj>, where MyObj is a object managing (for instance) a date, integer and double.
These are fragment of my code:
publicclass MyTableModelextends AbstractTableModel{
private String[] columnNames ={..};
private Vector<MyTableRow> rowData =new Vector<MyTableRow>();
public MyTableModel(){
rowData.addElement(new MyTableRow(new Date(), 0, 0));
}
public String getColumnName(int column){return columnNames[column];}
publicint getRowCount(){return rowData.size();}
publicint getColumnCount(){return columnNames.length;}
public Object getValueAt(int row,int col){return rowData.elementAt(row).get(col);}
}
class MyTableRow{
public MyTableRow(Date date,double a,int b){..}
public Object get(int col){
switch (col){
case 0:return date;
case 1:returnnew Double(a);
case 2:returnnew Integer(b);
default:thrownew IllegalArgumentException("Invalid "
+"access to row data for element at: " + col);
}
}
}
Now, how do I make when a cell is modified to have the correct field of my row object to be modified and how can I implement a "formattedfield" control for my 3 columns?
I have tried to use
getColumnModel().getColumn(5).
setCellRenderer(new IntegerRenderer());
publicclass IntegerRendererextends DefaultTableCellRenderer{
publicvoid setValue(Object value){..}
}
but if this works then it does not to modify my row object.
Please, clarify my doubts if possible.
Thank you very much for any help,
my best regards.

