1

我有一个使用 Json Jackson 从我的 Json 文件中获取的数据列表,如何从该列表中填充 jTable?

[{"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"}]

使用 Json Jackson 解析器,我用这些数据填充了列表。

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

现在我想填充 jTable,我使用了 NetBeans GUI 构建器来创建框架和表。表名是广告列表_JT。到目前为止,我尝试的是在网站上的一个类似问题中找到的这段代码。

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

有了这个片段,当我启动应用程序时,表格只是变灰,没有任何反应,通过调试器查看,也没有错误。

4

1 回答 1

1

我认为这是因为您没有提供表格标题。看看这是否有效:

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

另一种可能是广告列表为空;

于 2013-06-28T16:03:18.777 回答