title: The variables inside of the listener do not change
body: <p>I am trying to design a little Java software which involves JDBC and Swing. What my current problem is, I changed the model and table in one of the button listeners, but those two variables still remain the same in the other listener which is for the table. Both model and table are static, and can be accessed from any place in the class. I looked up the debugger, and it showed that variables are changed outside of the listener, but inside the listener, the variables are still not changed. Why would this happen? How to solve it?</p>
  <p>Probably this description is not clear enough, here's a more detailed process how the problem was generated:</p>
  <p>Here's where the table and model are stored, they are global and static:</p>
  <pre><code>public class MainView extends JFrame {
  
      private static UserService userService = new UserService();
      private static ItemService itemService = new ItemService();
      private static JTable table;
      private static JScrollPane tableScrollPane;
      private static NewTableModel model = new NewTableModel(null);
      private static JTextArea textArea = new JTextArea();
      public MainView() {
  
      }
      public void addFrm() {
        //here's where the main view window and listeners were put, this function will be called when the window needs to be displayed
      }
  }
  </code></pre>
  <p>Here is a table listener that finds which row is selected, and it can extract the text of the selected row. Every row corresponds to one class that stores a paragraph of text.</p>
  <pre><code>table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
      @Override
      public void valueChanged(ListSelectionEvent e) {
          if (!e.getValueIsAdjusting()) {
              if ((table.getSelectedRow() &gt; table.getRowCount()) || (table.getSelectedRow() &lt; 0)) {
                  textArea.setText(&quot;&quot;);
              }//every time after the table changed, the model will be changed, sometimes the row that user selected before the table might become unavailable which could cause invalid index
              else {
                  textArea.setText(model.getNote(table.convertRowIndexToModel(table.getSelectedRow())));
              }
          }
      }
  });
  </code></pre>
  <p>Here is the button listener that I mentioned before, when this button is clicked, a new window will be displayed and user can manipulate the MySQL database in that new window. This part should have no problem as the data can be written into the MySQL database correctly. After this window closed, a window listener is inside of it which will invoke the <code>update()</code> function when the new window is closing.</p>
  <pre><code>jb1.addActionListener(new ActionListener() {//Add some items
      @Override
      public void actionPerformed(ActionEvent actionEvent) {
          AddView addView = new AddView(&quot;adder&quot;);
          addView.addFrm();//new Window was stored in the other class
          addView.addWindowListener(new WindowAdapter() {
              @Override
              public void windowClosing(WindowEvent e) {
                  update();
                  super.windowClosing(e);
              }
          });
      }
  });
  </code></pre>
  <p>Here's the <code>update()</code> function:</p>
  <pre><code>public static void update(){
      String item = Objects.requireNonNull(comboBox.getSelectedItem()).toString();
       //this is used to select different item from catagories
      List&lt;ItemInfo&gt; list = itemService.getSelectedItem(item);
       //this is used to get a item list from database
      model.setItemInfo(list);//model has been override, it has a item list that stores list
      model.fireTableDataChanged();
      model.fireTableStructureChanged();
      table.setModel(model);//give a new model to the table
      table.validate();//update the table
      table.updateUI();
      tableScrollPane.validate();//table is put in a scrollpane, so update it as well
      tableScrollPane.updateUI();
      table.clearSelection();
  }
  </code></pre>
  <p>Here's the problem, if I select a row from the table, then I click the button and open a new window to add something. After I close that new window, that thing is added correctly. The model is also updated via update function, but an exception would appear after I closed the new window. It shows an IndexOutOfBoundsException.</p>
  <pre><code>Exception in thread &quot;AWT-EventQueue-0&quot; java.lang.IndexOutOfBoundsException: Index 18 out of bounds for length 18
      at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
      at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
      at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
      at java.base/java.util.Objects.checkIndex(Objects.java:372)
      at java.base/java.util.ArrayList.get(ArrayList.java:458)
      at com.Other.NewTableModel.getValueAt(NewTableModel.java:35)
  </code></pre>
  <p>However, if I only clicked the button, but did not select any row in the table, the program will work properly: the table will also be refreshed after the new window is closed.</p>
  <p>I checked the debugger, and found this is caused by the model that in the part of table listener, after I invoked <code>update()</code>, the model has been updated wherever it is, except the part in the listener. The model inside of this listener remains the same, and did not change. How could this happen? There is only one model which is totally the same variable and it is static and global.
  Anyone can help me about it?</p>
  <p>Some descriptions may not clear because I am not English speaker but I can explain if you ask more.</p>
  <p>some addition
  code NewTableModel:</p>
  <pre><code>import com.Model.ItemInfo;
  import javax.swing.table.AbstractTableModel;
  import java.util.List;
  
  public class NewTableModel extends AbstractTableModel {
  
      private List&lt;ItemInfo&gt; itemInfo;
      private String[] column = new String[]{&quot;Id&quot;,&quot;Name&quot;,&quot;Type&quot;,&quot;Number&quot;,&quot;Exp_Date&quot;,&quot;Adder&quot;,&quot;Exp.(day)&quot;,&quot;Added_Date&quot;};
  
      public NewTableModel(List&lt;ItemInfo&gt; itemInfo){
          this.itemInfo = itemInfo;
      }
  
      public void setItemInfo(List&lt;ItemInfo&gt; itemInfo) {
          this.itemInfo = itemInfo;
      }
  
      @Override
      public int getRowCount() {
          return itemInfo.size();
      }
  
      @Override
      public int getColumnCount() {
          return column.length;
      }
  
      @Override
      public Object getValueAt(int r, int c) {
          Object data = &quot;&quot;;
          switch (c){
              case 0:
                  data = itemInfo.get(r).getItem_id().toString();
                  break;
              case 1:
                  data = itemInfo.get(r).getItem_name();
                  break;
              case 2:
                  data = itemInfo.get(r).getItem_type();
                  break;
              case 3:
                  data = itemInfo.get(r).getItem_number().toString();
                  break;
              case 4:
                  if(itemInfo.get(r).getItem_date() == null){
                      return &quot;Not Available&quot;;
                  }
                  data = itemInfo.get(r).getItem_date().toString();
                  break;
              case 5:
                  data = itemInfo.get(r).getItem_adder();
                  break;
              case 6:
                  data = itemInfo.get(r).getItem_expDay();
                  break;
              case 7:
                  data = itemInfo.get(r).getItem_add_date();
          }
          return data;
  
      }
  
      @Override
      public String getColumnName(int column) {
          return this.column[column];
      }
  
      @Override
      public boolean isCellEditable(int rowIndex, int columnIndex) {
          return false;
      }
  
      public String getNote(int row){
          return itemInfo.get(row).getItem_notes();
      }
  
      public int getId(int row){
          return itemInfo.get(row).getItem_id();
      }
  }
  </code></pre>
  <p>Full Stark tracer is here:</p>
  <pre><code>Exception in thread &quot;AWT-EventQueue-0&quot; java.lang.IndexOutOfBoundsException: Index 19 out of bounds for length 19
      at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
      at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
      at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
      at java.base/java.util.Objects.checkIndex(Objects.java:372)
      at java.base/java.util.ArrayList.get(ArrayList.java:458)
      at com.Other.NewTableModel.getNote(NewTableModel.java:76)
      at com.View.MainView$2.valueChanged(MainView.java:163)
      at java.desktop/javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:219)
      at java.desktop/javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:186)
      at java.desktop/javax.swing.DefaultListSelectionModel.setValueIsAdjusting(DefaultListSelectionModel.java:723)
      at java.desktop/javax.swing.plaf.basic.BasicTableUI$Handler.setValueIsAdjusting(BasicTableUI.java:972)
      at java.desktop/javax.swing.plaf.basic.BasicTableUI$Handler.mouseReleased(BasicTableUI.java:1185)
      at java.desktop/java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:298)
      at java.desktop/java.awt.Component.processMouseEvent(Component.java:6651)
      at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
      at java.desktop/java.awt.Component.processEvent(Component.java:6416)
      at java.desktop/java.awt.Container.processEvent(Container.java:2263)
      at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5026)
      at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
      at java.desktop/java.awt.Component.dispatchEvent(Component.java:4858)
      at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
      at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
      at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
      at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
      at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2773)
      at java.desktop/java.awt.Component.dispatchEvent(Component.java:4858)
      at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:778)
      at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:727)
      at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
      at java.base/java.security.AccessController.doPrivileged(Native Method)
      at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
      at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
      at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:751)
      at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:749)
      at java.base/java.security.AccessController.doPrivileged(Native Method)
      at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
      at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:748)
      at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
      at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
      at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
      at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
      at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
      at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
  </code></pre>