0

我得到了一个类对象数组(名为:store)。我必须从存储数组中检索一些值,并希望用这些值填充我的 JTable(Object[][] 数据)。我已将此数组传递到一个类中,我计划在其中绘制我的用户界面,其中也包括表格。所以,我的代码看起来像

public class Dialog { // Here is where i plan to draw my UI (including the table)
....
    public Dialog(Store store) { // Store = an array of class object.
    .. }


    private class TableModel extends AbstractTableModel {

    private String[] columnNames = {"Selected ",
            "Customer Id ",
            "Customer Name "
    };
    private Object[][] data = {
            //  ???? 
    };
    }
}

现在,我的问题是,如果我想确保我的设计是一个好的设计并遵循 OOP 的原则,那么我应该在哪里从 store 中提取值,以及我应该如何将它传递给 data[][]。

4

1 回答 1

0

我会创建一个简单Object的表示Store(你甚至可以使用一个Properties对象或Map)。这将组成一个单独的行。

然后我会将每个“行”放入一个列表中......

protected class TableModel extends AbstractTableModel {

    private String[] columnNames = {"Selected",
            "Customer Id",
            "Customer Name"};

    private List<Map> rowData;

    public TableModel() {
        rowData = new ArrayList<Map>(25);
    }

    public void add(Map data) {
        rowData.add(data);
        fireTableRowsInserted(rowData.size() - 1, rowData.size() - 1);
    }

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

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

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

    public Object getValueAt(int rowIndex, int columnIndex) {
        Map row = rowData.get(rowIndex);
        return row.get(getColumnName(columnIndex));
    }
 }

现在,显然,这是一个非常简单的例子,但我希望你明白

于 2012-10-23T05:54:28.883 回答