Joã*_*pes 3 java swing jtable tablemodel
我正在为我的客户开发一个服务器JTable.
当我开始使用表模型时,我刚刚完成了列模型.与表模型中的列相关的大多数函数实际上是列模型中函数的别名.
无论如何,发生了一些奇怪的事情.我希望有人可以帮助我:
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;
}
Run Code Online (Sandbox Code Playgroud)
列模型
/**
* 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);
}
Run Code Online (Sandbox Code Playgroud)
那个硬编码的测试.2行,每行3个单元:
System.out.println("=========> " + model.getValueAt(1, 2)); // Outputs 'Cell 1,2'
Run Code Online (Sandbox Code Playgroud)
每行的第一个单元格的值是正确的.但对同一行中的所有其他单元格保持不变.
您的TableColumn创建不正确.
TableColumn包含TableModel的索引,其中列包含要显示的数据.创建TableColumn时,此值默认为0(因此所有列上都显示相同的数据).
我没有在你的代码中看到你实际创建TableColumn但你不应该使用的任何地方:
TableColumn tc = new TableColumn();
Run Code Online (Sandbox Code Playgroud)
相反,你应该使用:
TableColumn tc = new TableColumn( modelIndex );
Run Code Online (Sandbox Code Playgroud)