tolkinski
tolkinski

Reputation: 290

Populating jTable using List

I have a list of data which I got from my Json file using Json Jackson, how can I populate jTable from this list?

[{"id":1,"name":"Bambola","description":"Opis...","contact_number":"022\/349-499","email":"","address":"Svetosavksa 23","geo_latitude":"44.96868000000000","geo_longitude":"20.28140000000000","created_at":"2013-06-24 14:15:08","updated_at":"2013-06-24 14:15:08","deleted_at":null,"publication_starts":"1991-05-24 01:00:00","publication_ends":"1991-05-24 01:00:00"},{"id":2,"name":"Master","description":"Opis...","contact_number":"022\/349-123","email":"","address":"Svetosavksa 24","geo_latitude":"44.96653000000000","geo_longitude":"20.28170000000000","created_at":"2013-06-24 14:15:08","updated_at":"2013-06-24 14:15:08","deleted_at":null,"publication_starts":"0000-00-00 00:00:00","publication_ends":"0000-00-00 00:00:00"},{"id":3,"name":"Tritel","description":"Opis...","contact_number":"022\/321-499","email":"","address":"Svetosavksa 25","geo_latitude":"44.96654000000000","geo_longitude":"20.28170000000000","created_at":"2013-06-24 14:15:08","updated_at":"2013-06-24 14:15:08","deleted_at":null,"publication_starts":"0000-00-00 00:00:00","publication_ends":"0000-00-00 00:00:00"}]

Using the Json Jackson parser I have populated the List with this data.

List<Advertisement> advertisements = mapper.readValue(url, new TypeReference<List<Advertisement>>(){});

Now I want to populate the jTable, I have used the NetBeans GUI builder to create frame and the table. The table name is advertisementList_JT. So far what I have tried is this snippet of code found in a simillar question here on the site.

DefaultTableModel model = new DefaultTableModel();
        for (Advertisement adv : advertisements) {
            Object[] o = new Object[3];
            o[0] = adv.getName();
            o[1] = adv.getPublication_starts();
            o[2] = adv.getPublication_ends();
            model.addRow(o);
        }
        advertisementList_JT.setModel(model);

With this snippet the table when I start the application just goes gray and nothing happens, looked thru the debugger and no errors either.

Upvotes: 1

Views: 4342

Answers (1)

luke657
luke657

Reputation: 835

I think it happens because you didn't supply the table header. See if this works:

Object[] columnNames = {"Name", "Starts", "Ends"};
DefaultTableModel model = new DefaultTableModel(new Object[0][0], columnNames);
        for (Advertisement adv : advertisements) {
            Object[] o = new Object[3];
            o[0] = adv.getName();
            o[1] = adv.getPublication_starts();
            o[2] = adv.getPublication_ends();
            model.addRow(o);
        }
        advertisementList_JT.setModel(model);

Another possibility is that advertisements List is empty;

Upvotes: 1

Related Questions