3

私はjTableの実装と戦っています。独自の TableModel クラスを作成しました。そして問題があります。どういうわけか、私の tableData 配列 (Obejct[] の ArrayList) が正しく書き込まれていません。最後に、すべての行に値が含まれているテーブルを取得します。

ArrayList が正しく記述されていない理由は誰にも分かりますか?

class MyTableModel extends AbstractTableModel {
    private String[] columnNames = {"Auftragsnummer",
                                    "Kunde",
                                    "Kunden Nr.",
                                    "Erfasst",
                                    "Kommt",
                                    "Geht",
                                    "Kommentar"};

    String[] temp_delete = new String[10];
    int index_delete = 0;

    private ArrayList<Object[]> tableData = new ArrayList<Object[]>();

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return tableData.size();
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        Object [] temp = tableData.get(row);

        return temp[col];
    }

    public void removeAllEntry(){
        for (int i = 0; i < tableData.size(); i++) {
            tableData.remove(i);
        }
        model.fireTableDataChanged();
    }

     public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }


    public boolean isCellEditable(int row, int col) {

        switch (col){
            case 4:
                return true;
            default: return false;
        }
    }

     public void addText(Object[] object) {
        tableData.add(object);

       fireTableDataChanged();
      }

}

4

2 に答える 2

2

の親実装setValueAt()は何もしません。setValueAt()内部データ構造を更新するために実装する必要がありtableData、適切なを起動する必要がありTableModelEventます。これにより、ビューが更新されます。

    @Override
    public void setValueAt(Object aValue, int row, int col) {
        ... // update tableData
        this.fireTableCellUpdated(row, col); // notify the view
    }

余談ですが、List<List<Object>>の代わりに検討してArrayList<Object[]>ください。

于 2012-07-09T17:08:32.943 に答える
1

trashgodのおかげで私はそれを理解しました(彼の投稿を参照してください)。ここでの作業コード:

class MyTableModel extends AbstractTableModel {
    private String[] columnNames = {"Auftragsnummer",
                                    "Kunde",
                                    "Kunden Nr.",
                                    "Erfasst",
                                    "Kommt",
                                    "Geht",
                                    "Kommentar",
                                    "Abteilung"};

    String[] temp_delete = new String[10];
    int index_delete = 0;

   private ArrayList<ArrayList<Object>> tableData = new ArrayList<ArrayList<Object>>();


    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return tableData.size();

    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {

        if(tableData.size()> 0){
            return tableData.get(row).get(col);
        }

         return null;
    }

    public void removeAllEntry(){

            tableData.clear();
        model.fireTableDataChanged();
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        if(tableData.size()> 0){
            return getValueAt(0, c).getClass();
        }

        return String.class;
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.

        switch (col){
            case 4:
                return true;
            default: return false;
        }
    }


    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {

        if(tableData.size() <= row){

            ArrayList<Object> arrayList = new ArrayList<Object>();

            for(int i = 0; i < columnNames.length;i++){
                arrayList.add("");
            }

            tableData.add(arrayList);
        }

        ArrayList<Object> object = tableData.get(row);

        object.add(col, value);

        tableData.set(row, object);

        fireTableDataChanged();

    }

}

于 2012-07-10T13:13:45.573 に答える