0

I have many different array lists. I want each index to be a new row in the JTable but I'm not sure how to do that. I made a for loop but it is not working. Is there even a way to populate a JTable with an array list and not an array?

 public TableCreator() {
    super(new GridLayout(1,0));

    String[] columnNames = {"Item Type",
      "Description",
      "Size",
      "Price"};
    // for(int i=0; i<ShoppingFunctions.cartType.size(); i++){

    for(int i=0; i<GUI.size.size(); i++){//FIX!!!!
      item = ShoppingFunctions.cartType.get(i)+"\n";
      described = GUI.describe[GUI.imageNum];
      sizes = GUI.size.get(i);
      price = ShoppingFunctions.cartPrice.get(i)+"\n";
    }//end of for


   Object[][] data = {{item, described, sizes, price}};


    final JTable table = new JTable(data, columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    if (DEBUG){
      table.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e){
          printDebugData(table);
        }//end of method
      });//end of listener
    }//end of if

    JScrollPane scrollPane = new JScrollPane(table);
    add(scrollPane);
  }//end of method
jjj
  • 27
  • 8

2 Answers2

2

The simple answer is to create your own, based on something like AbstractTableModel

For example...

public abstract class AbstractGenericTableModel<R> extends AbstractTableModel {

    protected ArrayList<R> rows;
    protected List columnIdentifiers;

    public AbstractGenericTableModel() {
        this(0, 0);
    }

    public AbstractGenericTableModel(int rowCount, int columnCount) {
        this(new ArrayList(columnCount), rowCount);
    }

    public AbstractGenericTableModel(List columnNames, int rowCount) {
        setData(new ArrayList<>(rowCount), columnNames);
    }

    public AbstractGenericTableModel(List<R> data, List columnNames) {
        setData(data, columnNames);
    }

    public List<R> getRowData() {
        return rows;
    }

    private List nonNullVector(List v) {
        return (v != null) ? v : new ArrayList();
    }

    public void setData(List<R> data, List columnIdentifiers) {
        this.rows = new ArrayList<>(nonNullVector(data));
        this.columnIdentifiers = nonNullVector(columnIdentifiers);
        fireTableStructureChanged();
    }

    public void addRow(R rowData) {
        insertRow(getRowCount(), rowData);
    }

    public void insertRow(int row, R rowData) {
        rows.add(row, rowData);
        fireTableRowsInserted(row, row);
    }

    /**
     * Removes the row at <code>row</code> from the model. Notification of the row being removed will be sent to all the listeners.
     *
     * @param row the row index of the row to be removed
     * @exception ArrayIndexOutOfBoundsException if the row was invalid
     */
    public void removeRow(int row) {
        rows.remove(row);
        fireTableRowsDeleted(row, row);
    }

    public void setColumnIdentifiers(List columnIdentifiers) {
        setData(rows, columnIdentifiers);
    }

    @Override
    public int getRowCount() {
        return rows.size();
    }

    @Override
    public int getColumnCount() {
        return columnIdentifiers.size();
    }

    @Override
    public String getColumnName(int column) {
        Object id = null;
        // This test is to cover the case when
        // getColumnCount has been subclassed by mistake ...
        if (column < columnIdentifiers.size() && (column >= 0)) {
            id = columnIdentifiers.get(column);
        }
        return (id == null) ? super.getColumnName(column)
                        : id.toString();
    }

}

public class ArrayListTableModel extends AbstractGenericTableModel<ArrayList> {

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        List<ArrayList> rows = getRowData();
        return rows.get(rowIndex).get(columnIndex);
    }

}

This creates two classes, an "abstract generics" based model, which allows you to specify the physical data which backs a row and a ArrayListTableModel which allows you to use a ArrayList for the individual data.

What this assumes though, is each row has the same number of elements, but it makes no checks

I suggest you have a closer look at How to Use Tables for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

You can use the List Table Model. It will support rows of data stored in an ArrayList, Vector or any other class that implements the List interface.

The ListTableModel is also based on a generic TableModel that allows you to add Objects to a row in the model. There are many row based methods that allow easy usage of the model

The ListTableModel provides methods that allow you to easily customize the model:

  1. setColumnClass – specify the class of individual columns so the proper renderer/editor can be used by the table.
  2. setModelEditable – specify editable property for the entire model.
  3. setColumnEditable – specify editable property at a column level. This property will have preference over the model editable property.

The other option is to iterate through the ArrayList and copy each row of data to a Vector and then add the Vector to the DefaultTableModel using the addRow(...) method.

camickr
  • 321,443
  • 19
  • 166
  • 288