2012-10-23 16 views
0

我得到一個類對象(名爲:store)的數組。我不得不從商店數組中檢索一些值,並想用這些值填充我的JTable(Object [] []數據)。我已經將這個數組傳入了一個類,我正計劃繪製包含該表的用戶界面。所以,我的代碼看起來像如何在從AbstractTableModel擴展時正確填充表中的數據

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的prinicple又在哪裏正是我該提取店和i的值究竟是如何應將它傳遞給數據[] []。

+0

( s)你需要AbstractTableModel,作爲Object或Vector的Premature_Arrays,使用起來很簡單DefaltTableModel – mKorbel

回答

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

現在,很明顯,這是一個非常簡單的例子,但我希望你爲什麼理性觀念

相關問題