JTable, TableModel and TableColumnModel - weird what happens

I am developing costum JTable for my client.

I just finished the column model when I started working with the table model. Most of the functions associated with columns in a table model are actually aliases for functions in a column model.

In any case, something really strange happened. I hope someone can help me:

  • JTable shows the column correctly. This means that getColumnCount and getColumnName are working correctly.

  • The number of rows is displayed correctly. This means that getRowCount is working correctly.

  • The number of cells for each row is displayed correctly, since "getColumnCount" in the table model returns the value getColumnCount in the column model.

Now here is the strange thing:

The value for the first cell of each row is correct. But it remains the same for all other cells in the same row.

I assumed that, like most of you, I realized that getValueAt has something wrong. So, I decided to hard program the call after displaying the table. And guess what: the value returned correctly.

After some debugging, I found out that its JTable calling 'getValueAt (rowIndex, 0)', instead of 'getValueAt (rowIndex, columnIndex)'.

Can anyone help me with this? Regards...

MODEL TABLE

/**
 * 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;
}

COLUMN MODEL

/**
 * 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);
}

And this hard code. 2 rows with 3 cells each:

System.out.println("=========> " + model.getValueAt(1, 2)); // Outputs 'Cell 1,2'
+3
source share
2 answers

The value for the first cell of each row is correct. But it remains the same for all other cells in the same row.

Your TableColumn is not created correctly.

TableColumn TableModel, . TableColumn, 0 ( ).

, TableColumn, :

TableColumn tc = new TableColumn();

:

TableColumn tc = new TableColumn( modelIndex );
+5

, , , .

.

, . , . , , , . .

=)

0
source

Source: https://habr.com/ru/post/1788788/


All Articles