2011-01-30 75 views
3

我正在爲我的客戶開發一個costable JTable。JTable,TableModel和TableColumnModel - 奇怪的事情正在進行

剛開始使用表模型時,我剛剛完成了列模型。大多數與表模型中的列相關的函數實際上是列模型中的函數的別名。

無論如何,發生了一件非常奇怪的事情。我希望有人能幫助我:

  1. JTable顯示正確的列。這意味着getColumnCount和getColumnName正常工作。

  2. 行數顯示正確。這意味着getRowCount正常工作。

  3. 由於表模型中的「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' 

回答

5

爲每一行的第一個單元的值是正確的。但是對於同一行中的所有其他單元格,它仍然是一樣的。

您的TableColumn被錯誤地創建。

TableColumn包含TableModel的索引,其列包含要顯示的數據。當你創建一個TableColumn時,這個值默認爲0(所以相同的數據顯示在所有列上)。

我沒有看到任何地方在你的代碼中,實際上創建的TableColumn,但你不應該使用:

TableColumn tc = new TableColumn(); 

相反,你應該使用:

TableColumn tc = new TableColumn(modelIndex); 
+0

謝謝你,我很想知道那裏發生了什麼。 – flash 2011-08-26 07:37:05

0

表列索引未指定,因爲索引是動態更改的,因爲可以隱藏或在需要時將其設置爲可見。

這實際上是由表模型完成的。

無論如何,我想通了。我真的不知道發生了什麼事。我放棄了表模型,因爲我想我真的不需要它,並從頭開始創建表模型。現在工作正常。

無論如何=)