gpt4 book ai didi

java - 使用列表填充 jTable

转载 作者:太空宇宙 更新时间:2023-11-04 07:33:16 24 4
gpt4 key购买 nike

我有一个使用 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 构建器来创建框架和表。表名称为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);

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

最佳答案

我认为发生这种情况是因为您没有提供表头。看看这是否有效:

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

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

关于java - 使用列表填充 jTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17368505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com