使用List填充JTable [英] Populate JTable Using List

查看:257
本文介绍了使用List填充JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用具有对象类型的List中的值填充JTable。
我的代码如下所示:

How will I populate a JTable with values from a List with an Object Type. My Code looks like this :

String[] columnNames = {"CLASS CODE",
        "TIME",
        "DAY",
        "ROOM",
        "PROFESSOR"};

    List<org.mine.ScheduleAttr> schedule = getStudSched(studNo);
    DefaultTableModel model = new DefaultTableModel();
    table.setModel(model);

    model.setColumnIdentifiers(columnNames);

我已经有了列,列表会来自schedule变量吗?考虑到这些列,我如何把它放到我的桌子上?

I already have the columns, the list would come from the schedule variable ? How can I put that to my table considering these columns ?

推荐答案

看看DefaultTableModel 。您可以迭代List并为每一行创建Object数组。

Take a look at DefaultTableModel. You could iterate over your List and create the Object array for each row.

for (ScheduleAttr s : schedule) {
  Object[] o = new Object[5];
  o[0] = s.getX();
  o[1] = s.getY();
  o[2] = s.getZ();
  o[3] = s.getA();
  o[4] = s.getB();
  model.addRow(o);
}

这篇关于使用List填充JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆