0

TableModelを拡張する を作成していますAbstractTableModel。ただし、その内容は定期的に変更され、表示されるデータに応じて列の数が変わります。ComboBox場合によっては、列 2 のセルを編集するために を使用する必要がありComboBox、列 3 のセルに対して を使用する必要がある場合もあります。

次のようにして、テーブルのデフォルトのレンダラーを設定できることを知っていますtable.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(myComboBox));

を呼び出したときに が更新されるようにCellEditorTableModelを動的に設定できますか?CellEditortable.updateUI()

編集:私は基本的に、私が使用してTableModelいる大きなWellCollectionデータ構造を渡しています。詳細であなたを退屈させるリスクがありますが、それは でWells構成AttributesされていますProposedValues。しかし、それはあまり重要ではありません。重要な部分は、ユーザーが属性をめくってProposedValues、[次へ] をクリックしたときにテーブルに別の表示を表示できるようにすることです (以前に で実現しましたupdateUI())。

Attributeは立面タイプであっても、立面タイプでなくてもよいことに注意してください。立面タイプの場合は、追加の列が必要です。

これが私のコードです:

public class ProposedValueTableModel extends AbstractTableModel{
    private WellCollection wells;

    public ProposedValueTableModel(WellCollection wells){
        this.wells = wells;
    }


    @Override
    public int getColumnCount() {
        // if we're dealing with elevation values, we want fields for the value, the elevation type,
        // the source, and additional comments. If not, only show value, source, and comments.
        if(wells.getCurrAttribute().isElevationType())
            return 4;
        else return 3;
    }

    @Override
    public int getRowCount() {
        //NOTE: add 1 for extra, blank row! 
        return wells.getCurrAttribute().getProposedValues().size()+1;
    }

    @Override
    public Object getValueAt(int row, int col) {
        //TODO: convert this to handy dandy switch statements
        // make the last row blank to allow entries
        if (row==wells.getCurrAttribute().getProposedValues().size())
            return "";
        ProposedValue propVal = wells.getCurrAttribute().getProposedValues().get(row);
        //if we're NOT dealing with an elevation type, simply have three fields
        if(wells.getCurrAttribute().isElevationType()==false){
            switch(col){
            case 0:
                return propVal.val;
            case 1:
                return propVal.source;
            case 2:
                return propVal.comment;

            }
        }
        // if it IS an elevation value, include elevation type
        else{
            switch(col){
            case 0:
                    return propVal.val;
                case 1:
                    return propVal.elevType;
                case 2:
                    return propVal.source;
                case 3:
                    return propVal.comment;
            }
        }
        return "";
    }

    public String getColumnName(int col){

        if(wells.getCurrAttribute().isElevationType() ==false){
            switch(col){
                case 0:
                    return "Proposed value";
                case 1:
                    return "Source";
                case 2:
                    return "Comment";
            }
        }
        else{
            switch(col){
                case 0:
                    return "Proposed value";
                case 1:
                    return "Type";
                case 2:
                    return "Source";
                case 3:
                    return "Comment";
            }
        }
        return "header";
    }

    public boolean isCellEditable(int row, int col){
        return true;
    }

    public void setValueAt(Object value, int row, int col){
        // we're adding something to the last row, then add a new ProposedValue to the proposedValues array
        if(row == wells.getCurrAttribute().getProposedValues().size()){
            wells.getCurrAttribute().getProposedValues().add(new ProposedValue());
        }
        ProposedValue propVal = wells.getCurrAttribute().getProposedValues().get(row);
        if(wells.getCurrAttribute().isElevationType()==false){
            switch(col){
                case 0:
                    // Value
                    propVal.val = (String)value; break;
                case 1:
                    // Source
                    propVal.source = (String)value; break;
                case 2:
                    // Comment
                    propVal.comment = (String)value; break;
            }
        }
        else{
            switch(col){
                case 0:
                    // Value
                    propVal.val = (String)value; break;
                case 1:
                    // Elevation type
                    propVal.elevType = (String)value; break;
                case 2:
                    // Source
                    propVal.source = (String)value; break;
                case 3:
                    // Comment
                    propVal.comment = (String)value; break;
            }
        }
        //TODO: find out why this is necessary
        fireTableCellUpdated(row, col);
    }
}
4

2 に答える 2

2

table.updateUI() を呼び出したときに CellEditor が更新されるように、TableModel 内で CellEditor を動的に設定できますか?

  • そうではありません。これは JTables ビューを更新するための適切な方法ではありません

AbstractTableModel を拡張する TableModel を作成しています。ただし、その内容は定期的に変更され、表示されるデータに応じて列の数が変わります。場合によっては、列 2 のセルを編集するために ComboBox を使用する必要があり、列 3 のセルに対して ComboBox が必要になることもあります。

TableModel 内で CellEditor を動的に設定できますか

  • XxxTableModel が JComboBox の文字列値のみを XxxTableCellEditor として格納していることに注意してください

より良いヘルプのために、SSCCE、短く、実行可能で、コンパイル可能で、次のように保存するためのハードコードされた値とほぼJFrame一緒JTableに投稿してくださいJScrollPaneAbstractTableModellocal variable

于 2013-04-08T16:58:28.680 に答える