Java JTable、TableModel和TableColumnModel——奇怪的事情正在发生
我正在为我的一个客户开发Costam JTable 我刚完成列模型,就开始使用表模型。与表模型中的列相关的大多数函数实际上是列模型中函数的别名 总之,发生了一件非常奇怪的事。我希望有人能帮我:Java JTable、TableModel和TableColumnModel——奇怪的事情正在发生,java,swing,jtable,tablemodel,Java,Swing,Jtable,Tablemodel,我正在为我的一个客户开发Costam JTable 我刚完成列模型,就开始使用表模型。与表模型中的列相关的大多数函数实际上是列模型中函数的别名 总之,发生了一件非常奇怪的事。我希望有人能帮我: JTable正确显示列。这意味着getColumnCount和getColumnName工作正常 行数显示正确。这意味着getRowCount工作正常 由于表模型中的«getColumnCount»返回列模型中getColumnCount的值,因此正确显示了每行的单元格数 现在,奇怪的事情来了: 每行第一
/**
* 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);
}
每行第一个单元格的值是正确的。但对于同一行中的所有其他单元格,它保持不变
您的TableColumn创建不正确
TableColumn包含TableModel的索引,TableModel的列包含要显示的数据。创建TableColumn时,此值默认为0(因此所有列上都显示相同的数据)
我看不到您的代码中有任何地方实际创建了TableColumn,但您不应该使用:
TableColumn tc = new TableColumn();
TableColumn tc = new TableColumn( modelIndex );
相反,您应该使用:
TableColumn tc = new TableColumn();
TableColumn tc = new TableColumn( modelIndex );
未指定表列索引,因为索引是动态更改的,因为在需要时可以隐藏列或将列设置回可见 这实际上是由表模型完成的 不管怎样,我知道了。我真的不知道发生了什么。我放弃了表格模型,因为我觉得我真的不需要它,我从头开始做表格模型。现在很好用
无论如何,谢谢=)@flash,很高兴你能赏识这个答案,即使原始海报没有:)这真的很有帮助。谢谢