0

I want to create JTable that is initially empty. Then a user may load data from CSV file. So, it means that the number of rows in rowData is not known apriori. How should I change this line Object rowData[][] = new Object[][]{}; to fit my goal?

import javax.swing.table.AbstractTableModel;

class PaxListTableModel extends AbstractTableModel {

  Object rowData[][] = new Object[][]{};

  String columnNames[] = Constants.columnNamesPax;

  public int getColumnCount() {
    return columnNames.length;
  }

  public String getColumnName(int column) {
    return columnNames[column];
  }

  public int getRowCount() {
    return rowData.length;
  }

  public Object getValueAt(int row, int column) {
    return rowData[row][column];
  }

  public Class getColumnClass(int column) {
    return (getValueAt(0, column).getClass());
  }

  public void setValueAt(Object value, int row, int column) {
    rowData[row][column] = value;
  }

  public boolean isCellEditable(int row, int column) {
    return (column != 10);
  }
}
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • 2
    Does OpenCSV fill your needs? [Import CSV to JTable](http://stackoverflow.com/questions/10466079/import-csv-to-jtable) & [OpenCSV project](http://opencsv.sourceforge.net/) – Alexander Ortiz Oct 21 '15 at 09:42
  • Arrays are a poor choice for this; use `List`, for [example](http://stackoverflow.com/a/13919878/230513). – trashgod Oct 21 '15 at 09:59
  • @trashgod: In this case where do I populate the table model with the data from CSV? – Klausos Klausos Oct 21 '15 at 10:05
  • @Alexander Ortiz: I already do this, but the question is how to put all things together: CSV reader = new CSVReader(new FileReader("MyList.csv")); List allUsers = reader.readAll(); tableUsersModel.setRowCount(0); for (String[] obj: allUsers) tableUsersModel.insertRow(tableUsers.getRowCount(),obj); – Klausos Klausos Oct 21 '15 at 10:06
  • Given a `List` of parsed data, you don't have to, for [example](http://stackoverflow.com/a/17384208/230513). – trashgod Oct 21 '15 at 10:15

0 Answers0