-1

I'm trying to populate my DefaultTableModel by first taking a populated List collection and then converting them to an array of Object.

The problem begins when I addRow(Object[]) into DefaultTableModel. It always comes up empty. To be more precise, the DefaultTableModel is populated with rows, but the rows are empty, no columns or anything.

My main objective is to eventually put the contents of the List in DefaultTableModel to be able to put that in a JTable using setModel().

Source code:

List<Inventory> list = app.dao.getInventoryList(false, 0);
Object[] o = null;

for (Inventory i : list) {
    o = new Object[6];
    o[0] = i.getID();
    o[1] = i.getGoodName();
    o[2] = i.getCreateTime();
    o[3] = i.getCreateUser();
    o[4] = i.getUpdateTime();
    o[5] = i.getUpdateUser();
    app.dtInventory.addRow(o);
}

app.inventoryTableView.setModel(app.dtInventory);
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Ludwi
  • 437
  • 4
  • 8
  • 4
    Perhaps someone else can see the problem, but for me, I can't. Most will suggest that you'll have better chances of our being able to help if you create and post an [sscce](http://sscce.org). Also, be sure to test each step in your process above in isolation to be sure that you isolate precisely just where the error is. Much luck! – Hovercraft Full Of Eels Jan 26 '13 at 14:36
  • 1
    See also the problem encountered [here](http://stackoverflow.com/q/11223586/230513). – trashgod Jan 26 '13 at 15:51
  • 2
    Could also be that you don't initialize the columns by either specifying the columnNames in the Constructor or by callnig setColumnCount. – Guillaume Polet Jan 26 '13 at 17:00
  • yes.. i haven't initialized the columns.. thank you – Ludwi Feb 03 '13 at 08:22

1 Answers1

3

As you already have a List<Something>, make it the internal data model of your implementation of AbstractTableModel. There's an example here using a Map<String, String>.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045