-1

What I am trying to do is to populate a JTable from an ArrayList.

The array list is a type Record which I have defined below:

public class Record {

    int Parameter_ID;
    int NDC_Claims;
    int NDC_SUM_Claims;

    public Record(int parameter, int claims, int ndc_sum_claims){
        Parameter_ID = parameter;
        NDC_Claims = claims;
        NDC_SUM_Claims = ndc_sum_claims;

    }

    public Record() {
        // TODO Auto-generated constructor stub
    }

I don't know how to populate the table with the column headers as well. This is what I have so far:

DefaultListModel listmodel = new DefaultListModel();
ArrayList<Record> test = new ArrayList<Record>();
DefaultTableModel modelT = new DefaultTableModel();
    Object data1[] = new Object[3];

    for(int i=0;  i<test.size();i++){
        data1[0] = test.get(i).Parameter_ID;
        data1[1] = test.get(i).NDC_SUM_Claims;
        data1[2] = test.get(i).NDC_Claims;
        modelT.addRow(data1);
    }

table_1 = new JTable(modelT, columnNames);
contentPane.add(table_1, BorderLayout.CENTER);
contentPane.add(table_1.getTableHeader(), BorderLayout.NORTH);

Nothing is outputted. Any help would be great!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
sahandian
  • 3
  • 1
  • When you say there's no output, what do you mean? Does nothing happen when you run the program? Is there no table in your GUI? Is there a table, but it's empty? – Radiodef Jul 05 '18 at 21:27
  • 1
    *"Any help would be great!"* Any question would be great. Do you have a question? *"I don't know how to populate the table with the column headers as well."* Each thread on SO should include **one** clear, specific question. SO is a Q&A site, not a help desk. – Andrew Thompson Jul 06 '18 at 05:00

2 Answers2

1

Well you need to start by reading the API. You can't program if you don't read the API first.

DefaultTableModel modelT = new DefaultTableModel();

When you read the API what does that constructor do? It creates a model with 0 rows and 0 columns. You will want to create a model with 3 columns and 0 rows so that you can add rows of data to the model. Read the DefaultTableModel API

table_1 = new JTable(modelT, columnNames);

What does that statment do? I don't see a constructor that allows you to specify a model and column names so how does your code compile. You just want to create the table using the model.

contentPane.add(table_1, BorderLayout.CENTER);
contentPane.add(table_1.getTableHeader(), BorderLayout.NORTH);

The table should be added to the viewport of a JScrollPane. The header will then be displayed as the column header of the scroll pane.

Read the JTable API. The API also has a link to the Swing tutorial on How to Use Tables you need to read for the basics.

ArrayList<Record> test = new ArrayList<Record>();

You create an empty ArrayList. So what do you expect to happen when you iterate through the loop? How can you add data to the model if there is no data in the ArrayList?

Also, did you search the forum/web for examples that use the DefaultTableModel or JTable classes. Those examples will help you write your code.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you for the help. I apologize for asking such a silly question without clarifying it more. This information was very useful! Have a great weekend – sahandian Jul 06 '18 at 16:13
0

You can create a custom AbstractTableModel and then create a JTable using that model. Here is a class handling ArrayList of Arrays:

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

public class DataTableModel<T> extends AbstractTableModel {

/**
 * Benjamin Rathelot
 */
private static final long serialVersionUID = -7361470013779016219L;
private ArrayList<T[]> data = new ArrayList<T[]>();
private String[] tableHeaders;



public DataTableModel(ArrayList<T[]> data, String[] headers) {
    super();
    this.data = data;
    this.tableHeaders = headers;
}

@Override
public int getRowCount() {

    return data.size();
}

@Override
public int getColumnCount() {

    if(data.size()>0) return data.get(0).length;
    return 0;
}

@Override
public Object getValueAt(int arg0, int arg1) { 
    // TODO Auto-generated method stub
    if(data.size()>=arg0) {
        if(data.get(arg0).length>=arg1) {
            return data.get(arg0)[arg1];
        }
    }
    return null;
}

@Override
 public String getColumnName(int columnIndex) {
  return tableHeaders[columnIndex];
 }

}
Ben
  • 99
  • 1
  • 4