title: How to refresh data in JTable I am using TableModel
body: <p>
  Hi,</p>
  
  <p>I have created my TableModel and want to refresh JTable once I added a new row. What should be added to the listener to "refresh" JTable?</p>
  
  <pre><code>public class MyTableModel implements TableModel  {
      private Set&lt;TableModelListener&gt; listeners = new HashSet&lt;TableModelListener&gt;();
  
      //List&lt;Staff&gt; staffs = Factory.getInstance().getStaffDAO().getAllStaff();
      private List&lt;Staff&gt; staffs;
  
      public MyTableModel(List&lt;Staff&gt; staffs){
          this.staffs = staffs;
      }
  
      @Override
      public int getRowCount() {
          return staffs.size();
      }
  
      @Override
      public int getColumnCount() {
          return 5;  
      }
  
      @Override
      public String getColumnName(int columnIndex) {
          switch (columnIndex){
              case 0:
                  return "First Name";
              case 1:
                  return "Second Name";
              case 2:
                  return "Date";
              case 3:
                  return "Position";
              case 4:
                  return "Salary";
          }
          return "";  
      }
  
      @Override
      public Class&lt;?&gt; getColumnClass(int columnIndex) {
          return Object.class;  
      }
  
      @Override
      public boolean isCellEditable(int rowIndex, int columnIndex) {
          return true;  
      }
  
      @Override
      public Object getValueAt(int rowIndex, int columnIndex) {
          Staff staff = staffs.get(rowIndex);
          switch (columnIndex){
              case 0:
                  return staff.getName();
              case 1:
                  return staff.getSurname();
              case 2:
                  return staff.getDate();
              case 3:
                  return staff.getPosition();
              case 4:
                  return staff.getSalary();
          }
          return "";  
      }
  
      @Override
      public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
      }
  
      @Override
      public void addTableModelListener(TableModelListener l) {
      }
  
      @Override
      public void removeTableModelListener(TableModelListener l) {
      }
  }
  </code></pre>
  
  <p><strong>Here is my listener of my Add row Button:</strong></p>
  
  <pre><code> @Override
      public void actionPerformed(ActionEvent e) {
          Staff staff = new Staff();
          staff.setName(JOptionPane.showInputDialog("Enter First Name"));
          staff.setSurname(JOptionPane.showInputDialog("Enter Second Name"));
          staff.setDate(JOptionPane.showInputDialog("Enter Date"));
          staff.setPosition(JOptionPane.showInputDialog("Enter Position"));
          staff.setSalary(JOptionPane.showInputDialog("Enter Salary"));
          try {
              Factory.getInstance().getStaffDAO().addStaff(staff);
          } catch (SQLException e1) {
              e1.printStackTrace();  
          }
  !!!Here should be some code that will be firing my table after adding new row!!!
  }
  </code></pre>
  
  <p>I've tried to use method firetabledatachanged() of AbstractTableModel in my actionPerformed() but with unluck, it is appeared ClassCastException.</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>WorkPlaceGui.java</p>
  
  <pre><code>public class WorkPlaceGui extends JFrame implements ActionListener {
  
      AbstractTableModel model;
      JTable jTable;
      JScrollPane jScrollPane;
  
      public WorkPlaceGui()throws SQLException{
  
  
          List&lt;Staff&gt; staffs = Factory.getInstance().getStaffDAO().getAllStaff();
          for(int i = 0; i &lt; 0; i++) {
                  staffs.add(new Staff("First Name " + staffs.get(i).getName(), "Second Name " + staffs.get(i).getSurname(), "Date " + staffs.get(i).getDate(), "Position " + staffs.get(i).getPosition(), "Salary " + staffs.get(i).getSalary()));
          }
  
          model = new MyTableModel(staffs);
          jTable = new JTable(model);
          JButton jBtnAdd = new JButton("Добавить");
          JButton jBtnDel = new JButton("Удалить");
          JButton jBtnUpd = new JButton("Обновить");
          JButton jBtnAdmin = new JButton("Админка");
          JPanel panelNorth = new JPanel();
          JPanel panelCenter = new JPanel();
          JPanel panelSouth = new JPanel();
          jTable.setPreferredScrollableViewportSize(new Dimension(350, 150));
          jScrollPane = new JScrollPane(jTable);
  
  
          panelNorth.setLayout(new FlowLayout());
          panelNorth.add(jBtnAdd);
          panelNorth.add(jBtnDel);
          panelNorth.add(jBtnUpd);
          panelNorth.add(jBtnAdmin);
          panelCenter.add(jScrollPane);
  
          setLayout(new BorderLayout());
          add(panelNorth, BorderLayout.NORTH);
          add(panelCenter, BorderLayout.CENTER);
  
          jBtnAdd.addActionListener(this);
  
          setPreferredSize(new Dimension(550, 300));
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          setTitle("Staff data base");
          pack();
          setVisible(true);
          setLocationRelativeTo(null);
      }
  
      @Override
      public void actionPerformed(ActionEvent e) {
          Staff staff = new Staff();
          staff.setName(JOptionPane.showInputDialog("Enter First Name"));
          staff.setSurname(JOptionPane.showInputDialog("Enter Second Name"));
          staff.setDate(JOptionPane.showInputDialog("Enter Date"));
          staff.setPosition(JOptionPane.showInputDialog("Enter Position"));
          staff.setSalary(JOptionPane.showInputDialog("Enter Salary"));
          try {
              Factory.getInstance().getStaffDAO().addStaff(staff);
          } catch (SQLException e1) {
              e1.printStackTrace();
          }
          model.fireTableDataChanged();
      }
  }
  </code></pre>
  
  <p>MyTableModel.java</p>
  
  <pre><code>public class MyTableModel extends AbstractTableModel {
  
      private List&lt;Staff&gt; staffs;
  
      public MyTableModel(List&lt;Staff&gt; staffs){
          this.staffs = staffs;
      }
  
      @Override
      public int getRowCount() {
          return staffs.size();
      }
  
      @Override
      public int getColumnCount() {
          return 5;
      }
  
      @Override
      public Object getValueAt(int rowIndex, int columnIndex) {
          Staff staff = staffs.get(rowIndex);
          switch (columnIndex){
              case 0:
                  return staff.getName();
              case 1:
                  return staff.getSurname();
              case 2:
                  return staff.getDate();
              case 3:
                  return staff.getPosition();
              case 4:
                  return staff.getSalary();
          }
          return "";
      }
  }
  </code></pre>
  
  <p></p>