我正在爲我的客戶開發一個costable JTable。JTable,TableModel和TableColumnModel - 奇怪的事情正在進行
剛開始使用表模型時,我剛剛完成了列模型。大多數與表模型中的列相關的函數實際上是列模型中的函數的別名。
無論如何,發生了一件非常奇怪的事情。我希望有人能幫助我:
JTable顯示正確的列。這意味着getColumnCount和getColumnName正常工作。
行數顯示正確。這意味着getRowCount正常工作。
由於表模型中的「getColumnCount」返回列模型中的getColumnCount的值,因此每行的單元格數都正確顯示。
現在,來了奇怪的事情:
每一行的第一個單元格的值是正確的。但是對於同一行中的所有其他單元格,它仍然是一樣的。
我認爲,就像你已經做過的大部分事情那樣,getValueAt有問題。所以,我決定在表格渲染完成後對它進行硬編碼。並猜測:價值恢復正確。
經過一些調試後,我發現它的JTable調用'getValueAt(rowIndex,0)',而不是'getValueAt(rowIndex,columnIndex)'。
任何人都可以幫助我嗎?最好的問候......
臺式
/**
* Returns the value to be displayed for this column at this row index.
* @param rowIndex
* @param columnIndex
* @return
*/
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;
}
/**
* Returns the LOS_TableCell at the specified JTable indexes
* @param index
* @return
*/
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;
}
/**
* Returns the number of visible columns
* @return
*/
public int getColumnCount() {
int result = this.columnModel.getColumnCount();
System.out.println("LOS_TableModel : getColumnCount() : " + result);
return result;
}
/**
* Returns the total of displayed rows
* @return
*/
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;
}
列模型
/**
* Returns an enumeration of columns.
* @return
*/
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();
}
/**
* Used by the JTable to get a column index.
* @param columnIdentifier
* @return
*/
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;
}
/**
* Return a column using its JTable index
* @param columnIndex
* @return
*/
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);
}
這硬編碼的測試。 2排,每3個細胞:
System.out.println("=========> " + model.getValueAt(1, 2)); // Outputs 'Cell 1,2'
謝謝你,我很想知道那裏發生了什麼。 – flash 2011-08-26 07:37:05