title: How to update a jtable, when using a custom TableModel with a list of items read from a file
body: <p>i have a problem with my Jtable/TableModel that no matter how much i try i cant seem to fix. im working on an address book program, without a database program since our professor want us to learn everything by hand, so what i have is a .csv file that i use to read/write contacts on. i read the contacts in the file, put them on a list, use that list to form a TableModel around it, and then make a Jtable with said TableModel. my problem comes up, when if i write a new contact on the file, the jtable will not update itself with the new info, unless i close down the program and start it up again. how would i be able to to make the table update itself with the new info?</p>
  
  <p>heres where i first make the jtable and table model, wont post whole method since its nonrelated.</p>
  
  <pre><code>List&lt;Contatcs&gt; listContacts = createListContacts(username);
  tableModel = new TableModel(listContacts);
  
  //create table, sorter and set single selection model
  jTable = new JTable(tableModel);
  final TableRowSorter&lt;TableModel&gt; sorter = new TableRowSorter&lt;TableModel&gt;(tableModel);
  JTableHeader jtHeader = jTable.getTableHeader();
  </code></pre>
  
  <p>here's where i create my list of contacts </p>
  
  <pre><code>public static List&lt;Contatcs&gt; createListContacts(String username) throws FileNotFoundException{
  
          String file = "CustomersAddressBooks\\" + username + ".csv";
          Scanner fileReader = new Scanner(new File(file));
          List&lt;Contatcs&gt; retVal = new ArrayList();
          while (fileReader.hasNextLine()){
              String line = fileReader.nextLine();
              line = line.replaceAll("\r||\n","");
              String[] var = line.split(",");
              if(!line.equals("") &amp;&amp; !var[0].equals("FirstName") &amp;&amp; !var[1].equals("LastName")){
  
                  retVal.add(new Contatcs(var[0],var[1],var[2],var[3],var[4],var[5],var[6],var[7],var[8]));
              }
          }
  
          fileReader.close();
  
          return retVal;
      }
  </code></pre>
  
  <p>and this is my TableModel class</p>
  
  <pre><code>public class TableModel extends AbstractTableModel {
      private static final int COLUMN_FIRSTNAME = 0;
      private static final int COLUMN_LASTNAME = 1;
      private static final int COLUMN_PHONENUMBER = 2;
      private static final int COLUMN_CITY = 3;
  
      private String[] columnNames = {"First Name", "Last Name", "Phone Number", "City"};
      private List&lt;Contatcs&gt; listContacts;
  
      public TableModel(List&lt;Contatcs&gt; listContacts){
          this.listContacts = listContacts;
          int indexCount = 1;
          for(Contatcs contact : listContacts){
              contact.setIndex(indexCount++);
          }
      }
  
      @Override
      public int getColumnCount() {
          return columnNames.length;
      }
  
      @Override
      public int getRowCount() {
          return listContacts.size();
      }
  
      @Override
      public Object getValueAt(int rowIndex, int columnIndex) {
          Contatcs contacts = listContacts.get(rowIndex);
          Object retVal = null;
          switch(columnIndex){
          case COLUMN_FIRSTNAME: retVal = contacts.getFirstName(); break;
          case COLUMN_LASTNAME: retVal = contacts.getLastName(); break;
          case COLUMN_PHONENUMBER: retVal = contacts.getPhoneNumber(); break;
          case COLUMN_CITY: retVal = contacts.getCity(); break;
          default: throw new IllegalArgumentException("Invalid column index.");
          }
          return retVal;
      }
  
      @Override
      public String getColumnName(int columnIndex){
          return columnNames[columnIndex];
      }
      @Override
      public Class&lt;?&gt; getColumnClass(int columnIndex){
          if(listContacts.isEmpty()) return Object.class;
          else return getValueAt(0,columnIndex).getClass();
      }
  }
  </code></pre>