Arjel
Arjel

Reputation: 469

Populate JTable Using List

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 ?

Upvotes: 10

Views: 45724

Answers (3)

C12-H22-O11
C12-H22-O11

Reputation: 557

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

Upvotes: 8

Maguzu
Maguzu

Reputation: 433

try this

first make a iterator

Iterator itr = StringList.iterator();



 while(itr.hasNext()) {
                 Object element = itr.next();
                     int y=0;
                     for (y=0; y <=16 ; y++){
                    table_4.setValueAt(element, y, 0);

table_4 is your table name then set the row and column where you want to insert the string

Upvotes: 0

Francisco Spaeth
Francisco Spaeth

Reputation: 23913

You could use something similar to (just changing columns and values):

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class TestJFrame extends JFrame {
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        TestJFrame testJFrame = new TestJFrame();

        List<String> columns = new ArrayList<String>();
        List<String[]> values = new ArrayList<String[]>();

        columns.add("col1");
        columns.add("col2");
        columns.add("col3");

        for (int i = 0; i < 100; i++) {
            values.add(new String[] {"val"+i+" col1","val"+i+" col2","val"+i+" col3"});
        }

        TableModel tableModel = new DefaultTableModel(values.toArray(new Object[][] {}), columns.toArray());
        JTable table = new JTable(tableModel);
        testJFrame.setLayout(new BorderLayout());
        testJFrame.add(new JScrollPane(table), BorderLayout.CENTER);

        testJFrame.add(table.getTableHeader(), BorderLayout.NORTH);

        testJFrame.setVisible(true);
        testJFrame.setSize(200,200);
    }

}

The columns doesn't need to look like columns.toArray() because you already have an array of objects, so is just use it. At the end in order to use your columns the instruction looks like: TableModel tableModel = new DefaultTableModel(values.toArray(new Object[][] {}), columnNames);

Upvotes: 6

Related Questions