title: Swing : Single AbstractTableModel for multiple objects ??
body: <p>I have different objects which is used to store the data from DB in an <code>ArrayList&lt;T&gt;</code>. I got to show records of each in a Table. I use an <code>AbstractTableModel</code> for first object am working on. The <code>AbstractTableModel</code> class has an <code>ArrayList&lt;Relation&gt;</code> and an <code>String[]</code> of headers. </p>
  
  <p>My question is : How do I create the same <code>AbstractTableModel</code> that can be used for all objects. I mean if working with Relation, I can have <code>ArrayList&lt;Relation&gt;</code>, if with Product, can have <code>ArrayList&lt;Product&gt;</code> ..etc. Writing different <code>AbstrastTableModel</code> for each is not a good idea according to me. The main problem comes with <code>setValueAt</code>, <code>getValue</code>, <code>addRow</code>.... </p>
  
  <pre><code>    @Override
  public void setValueAt(Object value, int row, int col) {
      Relation r = data.get(row);     // ArrayList&lt;Relation&gt; data
      switch (col) {
          case 0:
              r.setId(value.toString());
              break;
          case 1: r.setName(value.toString());
              break;
          case 2: r.setStartBalance((Double)value);
              break;
          case 3: r.setCurrentBalance((Double)value);
              break;
      }
  }
  </code></pre>
  
  <p>Any good idea to work on this as a single model that works for all. And can also fire events from here for the table.</p>
  
  <p>I know it is a bit complicated. But the structure is vast and creating new class of <code>AbstractTableModel</code> and <code>JTable</code> for each object is also not a good idea. </p>
  
  <p>What do you suggest ?</p>