2

助けてください。jtable から 2 つのセル、1 つの ID と 1 つの説明があります。ID と説明は両方ともカスタム コンボボックスです。私がやろうとしているのは、ID がフォーカスを失ったり値が変更されたりしたときに、ID の値に基づいて説明が更新されることです。それ、どうやったら出来るの?

両方のセルを実装するための私のコードは次のとおりです。

TableColumn subAccountCol = jTable1.getColumnModel().getColumn(table.findColumn("SubAccount"));

    javax.swing.JComboBox accountCbx = new javax.swing.JComboBox(Account.toArray());
    javax.swing.JComboBox accountDescCbx = new javax.swing.JComboBox(AccountDesc.toArray());

    CompleteText.enable(accountCbx);
    CompleteText.enable(accountDescCbx);

    jTable1.getColumnModel().getColumn(table.findColumn("Account")).setCellEditor(new ComboBoxCellEditor(accountCbx));
    jTable1.getColumnModel().getColumn(table.findColumn("Account Description")).setCellEditor(new ComboBoxCellEditor(accountDescCbx));
4

1 に答える 1

3

setValueAt()セルエディタは、最終的にテーブルモデルのメソッドを呼び出します。このテーブルモデルでは、編集されたセル値に加えてリンクされたセル値を更新し、両方のセルに対して適切な変更イベントを発生させます。

public MyTableModel extends AbstractTableModel() {
    // ...

    // modifies the value for the given cell
    @Override
    public void setValueAt(Object value, int row, int column) {
        Foo foo = this.list.get(row);
        if (column == INDEX_OF_ID_COLUMN) {
            foo.setId(value); // change the ID
            fireTableCellUpdated(row, column); // signal the the ID has changed
            // and now also change the description
            String newDescription = createNewDescription(value);
            foo.setDescription(newDescription);
            fireTableCellUpdated(row, INDEX_OF_DESCRIPTION_COLUMN); // signal the the description has changed  
        }
        // ...
    }
}
于 2012-05-14T08:34:06.867 に答える