簡體   English   中英

JTable,TableModel和TableColumnModel - 發生了奇怪的事情

[英]JTable, TableModel and TableColumnModel - Weird thing going on

我正在為我的客戶開發一個服務器JTable。

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

無論如何,真的很奇怪。 我希望有人可以幫助我:

  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'

每行的第一個單元格的值是正確的。 但對同一行中的所有其他單元格保持不變。

您的TableColumn創建不正確。

TableColumn包含TableModel的索引,其中列包含要顯示的數據。 創建TableColumn時,此值默認為0(因此所有列上都顯示相同的數據)。

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

TableColumn tc = new TableColumn();

相反,你應該使用:

TableColumn tc = new TableColumn( modelIndex );

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

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

無論如何,我想通了。 我真的不知道發生了什么。 我放棄了桌面模型,因為我認為我真的不需要它並從頭開始做表格模型。 現在工作正常。

非常感謝=)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM