使用List填充jTable

时间:2013-06-28 15:37:50

标签: java json list jtable jackson

我有一个使用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。

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

现在我想填充jTable,我已经使用NetBeans GUI构建器来创建框架和表格。表名是advertisementList_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);

使用此代码片段,当我启动应用程序时,表格变为灰色且没有任何反应,通过调试器查看,也没有错误。

1 个答案:

答案 0 :(得分: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);

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