I am developing costum JTable for my client.
I just finished the column model when I started working with the table model. Most of the functions associated with columns in a table model are actually aliases for functions in a column model.
In any case, something really strange happened. I hope someone can help me:
JTable shows the column correctly. This means that getColumnCount and getColumnName are working correctly.
The number of rows is displayed correctly. This means that getRowCount is working correctly.
The number of cells for each row is displayed correctly, since "getColumnCount" in the table model returns the value getColumnCount in the column model.
Now here is the strange thing:
The value for the first cell of each row is correct. But it remains the same for all other cells in the same row.
I assumed that, like most of you, I realized that getValueAt has something wrong. So, I decided to hard program the call after displaying the table. And guess what: the value returned correctly.
After some debugging, I found out that its JTable calling 'getValueAt (rowIndex, 0)', instead of 'getValueAt (rowIndex, columnIndex)'.
Can anyone help me with this? Regards...
MODEL TABLE
public Object getValueAt(int rowIndex, int columnIndex) {
System.out.println(String.format("LOS_TableModel: getValueAt(%d, %d)", rowIndex, columnIndex));
LOS_TableCell cell = this.getCell(columnIndex, rowIndex);
if(cell == null) return null;
else return cell.value;
}
public LOS_TableCell getCell(int columnIndex, int rowIndex) {
for(Object o_row : this.rows) {
if(o_row.getClass() == LOS_TableRowGroup.class) {
LOS_TableRowGroup row = (LOS_TableRowGroup) o_row;
LOS_TableCell cell = row.getCell(columnIndex, rowIndex);
if(cell != null) return cell;
}
else {
LOS_TableRow row = (LOS_TableRow) o_row;
for(LOS_TableCell cell : row.cells)
if(cell.column.tableIndex == columnIndex && cell.row.tableIndex == rowIndex) return cell;
}
}
return null;
}
public int getColumnCount() {
int result = this.columnModel.getColumnCount();
System.out.println("LOS_TableModel : getColumnCount() : " + result);
return result;
}
public int getRowCount() {
int rowCount = 0;
for(LOS_TableRow row : this.rows) {
if(row.visible) rowCount += 1;
if(row.getClass() == LOS_TableRowGroup.class)
rowCount += ((LOS_TableRowGroup) row).getDisplayedRowCount();
}
return rowCount;
}
COLUMN MODEL
public Enumeration<TableColumn> getColumns() {
Vector<TableColumn> columns = new Vector<TableColumn>();
for(LOS_TableColumn column : this.columns)
if(column.visible) columns.add((TableColumn)column);
return columns.elements();
}
public int getColumnIndex(Object columnIdentifier) {
System.out.println("LOS_ColumnModel: getColumnIndex(" + columnIdentifier + ")");
for(LOS_TableColumn column : this.columns)
if(column.getIdentifier().equals(columnIdentifier)) return column.tableIndex;
return -1;
}
public TableColumn getColumn(int columnIndex) {
System.out.println("LOS_ColumnModel : getColumn(" + columnIndex + ")");
for(LOS_TableColumn column : this.columns)
if(column.tableIndex == columnIndex) return column;
throw new IndexOutOfBoundsException("" + columnIndex);
}
And this hard code. 2 rows with 3 cells each:
System.out.println("=========> " + model.getValueAt(1, 2));