私は私のクライアントのために costum JTable を開発しています。
テーブル モデルを開始したとき、列モデルを完成させたばかりでした。テーブル モデルの列に関連するほとんどの関数は、実際には列モデルの関数のエイリアスです。
とにかく、本当に奇妙なことが起こりました。誰かが私を助けてくれることを願っています:
JTable は列を正しく表示します。これは、getColumnCount と getColumnName が正しく機能していることを意味します。
行数が正しく表示されます。これは、getRowCount が適切に機能していることを意味します。
テーブル モデルの «getColumnCount» が列モデルの getColumnCount の値を返すため、各行のセル数が適切に表示されます。
さて、ここで奇妙なことが起こります:
各行の最初のセルの値は正しいです。ただし、同じ行の他のすべてのセルについては同じままです。
ほとんどの人がすでにそうしているように、getValueAt には何か問題があると思いました。そこで、テーブルがレンダリングされた後に呼び出しをハードコーディングすることにしました。そして、何を推測しますか: 値が正しく返ってきました。
いくつかのデバッグの後、「getValueAt(rowIndex, columnIndex)」ではなく「getValueAt(rowIndex, 0)」を呼び出している JTable であることがわかりました。
誰でもこれで私を助けることができますか?よろしくお願いします...
テーブルモデル
/**
* 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);
}
そして、そのハードコードされたテスト。それぞれ 3 つのセルを含む 2 つの行:
System.out.println("=========> " + model.getValueAt(1, 2)); // Outputs 'Cell 1,2'