0

I am getting an array of class object (named: store). I have to retrive some values from the store array and want to fill my JTable (Object[][] data) with those values. I have passed this array into a class in which i am planning to draw my user interface which includes the table too. So, my code looks like

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 = {
            //  ???? 
    };
    }
}

Now, my question is if i want to make sure my design is a good design and follows prinicple of OOP then where exactly shall i extract the values from store and how exactly should i pass it into data[][].

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mu_sa
  • 2,685
  • 10
  • 39
  • 58
  • for why reason(s) you need AbstractTableModel, for Premature_Arrays as Object or Vector, simple to use DefaltTableModel – mKorbel Oct 23 '12 at 07:04

1 Answers1

0

I would create a simple Object representation of the Store (you could even use a Properties object or Map). This will make up an individual row.

I would then place each "row" into a list...

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

Now, obviously, this is a pretty simple example, but I hope you get the idea

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366