Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
413 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...