我有從Json文件中使用Json Jackson獲得的數據列表,我如何從這個列表填充jTable?使用列表填充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);
在這個片段表時我啓動應用程序只是去灰,什麼也沒有發生,看着直通調試器,沒有錯誤或者。
看看AbstractTableModel,這裏是最近兩個月的一些非常好的嘗試 – mKorbel
你發佈的基本代碼看起來很好,但我們不知道代碼被調用的上下文。如果框架是灰色的,那麼我會建議你不要將表格添加到框架中。也許你有實例和局部變量。添加一些調試語句給你的代碼看看你的代碼是否被執行。 – camickr
@mKorbel:你爲什麼不喜歡DefaultTableModel?大多數表格不需要自定義表格模型。 –