title: JTable (BeanTableModel) not updating/refreshing - Java Swing
body: <p>I've 2 panels. One where the user inserts the data and the other with a <code>JTable</code> where the results will be shown.</p>
  
  <p>My problem is when the user press's the ok (in my case apply) <code>JButton</code> the data is computed but is not shown on the <code>JTable</code>, nothing changes in the <code>JTable</code>.</p>
  
  <p>For my <code>JTable</code> I'm using the <code>Bean Table Model</code> from tips4Java (<a href="http://tips4java.wordpress.com/2008/11/27/bean-table-model/" rel="nofollow">http://tips4java.wordpress.com/2008/11/27/bean-table-model/</a>). </p>
  
  <p>One weird thing is if I send data (lets call this data 'A') to the table when the program starts it is shown on the table and if later on I try to update the table, the table does not update. But when I don't send data to the table at start up but try to update/send the table with data 'A' it does not update.</p>
  
  <p>So my question is, why is not the <code>JTable</code> showing whatever data I send to?</p>
  
  <p>Here's my code:</p>
  
  <p>JButton listenere that starts the processing and sends the data to the table:</p>
  
  <pre><code>crashForm.setFormListener(new FormListener() {
      @Override
      public void formEvent(OptionsFormEvent oe) {
          String readTable = oe.getReadFromTable();
          int access = oe.getAccess();
          int transition = oe.getTransition();
          boolean smooth = oe.isTrainCrash();
          ArrayList&lt;String&gt; allTrains = new ArrayList&lt;&gt;();
          List crashedTrainList = new ArrayList&lt;&gt;();
  
          try {
              allTrains = controller.getUniqueTrains(controller.connectServer(), readTable, "trainid");
          } catch (Exception ex) {
              JOptionPane.showMessageDialog(null, "An error occured while getting trains data\n"
                      + "Error description: " + ex.getMessage(), "Error",
                      JOptionPane.ERROR_MESSAGE);
          }
  
          try {
              for (int i = 0; i &lt; allTrains.size(); i++) 
              {
                  ArrayList&lt;Train&gt; trainDataList = new ArrayList&lt;&gt;();
                  ArrayList&lt;Train&gt; crashedProccessedData = new ArrayList&lt;&gt;();
  
                  String query = "the sql query...";
  
                  trainDataList = controller.getTrainData(controller.connectServer(), readTable, query);
  
                  crashedProccessedData = controller.detectCrash(access, transition, trainDataList);
                  crashedTrainList.addAll(crashedProccessedData);
  
              }
          } catch (Exception ex) {
              JOptionPane.showMessageDialog(null, "An error occured while detecting a crash.\n"
                      + "Error description: " + ex.getMessage(), "Error",
                      JOptionPane.ERROR_MESSAGE);
          }
  
          System.out.println("Total crashes detected:" + crashedTrainList.size());
          tablePanel.createTable(Train.class, crashedTrainList, true, false, tableLabels);
          tablePanel.fireTableDataChanged();
      }
  });
  }
  </code></pre>
  
  <p>And here's my tablePanel class:</p>
  
  <pre><code>    public TablePanel() {
      }
      public void createTable(Class c, List data, boolean toolBarUp, 
              boolean toolBarBottom, ArrayList&lt;String&gt; labelsCheckBox) {
  
          beanTableModel = new BeanTableModel(c, data);
          columnModel = new XTableColumnModel();
          table = new JTable(beanTableModel);
          table.setColumnModel(columnModel);
          table.createDefaultColumnsFromModel();
  
          if(toolBarUp == true)
          {
              final JToolBar toolBarTop = new JToolBar();
  
              // Create the Show ALL
              JButton reset = new JButton("Reset");
  
              reset.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
  
                  for(Component c : toolBarTop.getComponents()){
                      if(c instanceof JCheckBox){
                          JCheckBox checkBox = (JCheckBox) c;
                          checkBox.setSelected(false);
                          columnModel.setAllColumnsVisible();
                      }
                  }
                  int numberOfColumn = columnModel.getColumnCount();
  
                  for(int aux = 0; aux &lt; numberOfColumn; aux++)
                  {
                      int num = columnModel.getColumnCount();
                      TableColumn column = columnModel.getColumnByModelIndex(aux);
                      columnModel.setColumnVisible(column, true);
                  }
              }
              });
  
              toolBarTop.add(reset);
  
              // Create a JCheckBox for each column
              for(int i = 0; i &lt; labelsCheckBox.size(); i++)
              {
                  final int index = i;
                  toolBarTop.add(new JCheckBox(new AbstractAction(labelsCheckBox.get(i)) {
                      @Override
                      public void actionPerformed(ActionEvent e) {
                          TableColumn column = columnModel.getColumnByModelIndex(index);
                          boolean visible = columnModel.isColumnVisible(column);
                          columnModel.setColumnVisible(column, !visible);
                      }
                  }));
              }
  
  
  
              setLayout(new BorderLayout());
              add(toolBarTop, BorderLayout.NORTH);
              add(new JScrollPane(table), BorderLayout.CENTER);
  //            add(toolBarDown, BorderLayout.SOUTH);
          }
  
  
          final JToolBar toolBarDown = new JToolBar();
  
          toolBarDown.add(new JButton(new AbstractAction("Save Table") {
              @Override
              public void actionPerformed(ActionEvent e) {
                  throw new UnsupportedOperationException("Not supported yet.");
              }
          }));
  
  
      }
  
      public void createTable(Class c, Class cAncestor) {
          beanTableModel = new BeanTableModel(c, cAncestor);
          table = new JTable(beanTableModel);
          setLayout(new BorderLayout());
          add(new JScrollPane(table), BorderLayout.CENTER);
      }
  
      public void createTable(Class c, Class cAncestor, List data) {
          beanTableModel = new BeanTableModel(c, cAncestor, data);
          table = new JTable(beanTableModel);
          setLayout(new BorderLayout());
          add(new JScrollPane(table), BorderLayout.CENTER);
          beanTableModel.fireTableDataChanged();
      }
  
      public void createTable(Class c) {
          beanTableModel = new BeanTableModel(c);
          table = new JTable(beanTableModel);
          setLayout(new BorderLayout());
          add(new JScrollPane(table), BorderLayout.CENTER);
      }
  
      public void createTable(Class c, List data)
      {
          beanTableModel = new BeanTableModel(c, data);
          table = new JTable(beanTableModel);
          setLayout(new BorderLayout());
          add(new JScrollPane(table), BorderLayout.CENTER);
      }
  
      //to refresh the table everytime there is an update
      public void fireTableDataChanged()
      {
          beanTableModel.fireTableDataChanged();
      }
  </code></pre>
  
  <p>So again, my question is: Isn't my <code>JTable</code> not updated with the new results every time I send new data to it?</p>