Java 使用列表填充jTable
我有一个使用Json Jackson从Json文件中获取的数据列表,如何从该列表中填充jTableJava 使用列表填充jTable,java,json,list,jtable,jackson,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_
[{"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>>(){});
有了这个代码片段,当我启动应用程序时,表变成灰色,什么也没有发生,通过调试器查看,也没有错误。我认为这是因为您没有提供表头。看看这是否有效:
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);
另一种可能是广告列表为空 我认为这是因为您没有提供表头。看看这是否有效:
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);
另一种可能是广告列表为空 看看AbstractTableModel,在过去的两个月里,这里有一些非常好的尝试。您发布的基本代码看起来不错,但我们不知道如何调用代码的上下文。如果框架是灰色的,那么我建议您实际上不要将表添加到框架中。也许你有实例和局部变量。向代码中添加一些调试语句,看看代码是否正在执行。@mKorbel:为什么不喜欢DefaultTableModel?大多数表不需要自定义表模型。@ Gilbert Le Blanc肯定我喜欢Debug Tababl,但是我认为对于基于UTIL.List的数组来说,最好只查找ActudiTababMuldOL,然后使用列表作为下层数组直接查看AbstractTableModel,在过去的两个月里,这里有一些非常好的尝试。您发布的基本代码看起来不错,但我们不知道如何调用代码的上下文。如果框架是灰色的,那么我建议您实际上不要将表添加到框架中。也许你有实例和局部变量。向代码中添加一些调试语句,看看代码是否正在执行。@mKorbel:为什么不喜欢DefaultTableModel?大多数表不需要自定义表模型。@ Gilbert Le Blanc肯定我喜欢Debug TababMuldOL,但是我认为对于基于UTIL.List的数组来说,最好只查找ActudiTababMuldOL,然后将列表作为底层数组直接使用,谢谢。列名是个问题,我想如果我在GUI builder中编写了列名,它们将应用到表中,那么使用.setModel我将替换以前使用GUI builder完成的所有配置。是的,它工作正常,谢谢。列名是个问题,我不认为如果我在GUI builder中编写了列名,它们将应用于表,那么使用.setModel我将替换以前使用GUI builder完成的所有配置。