title: close window on button click
body: <p>Hello,</p>
  
  <p>I am using Java Swing and I want to close a window on a button click. I don't know using an actionlistener as the best way to do this but currently I am having compilation errors with it so its must be incorrect.</p>
  
  <p>Here my code:</p>
  
  <pre><code>    public class assignment2
      {
  
          public static void main(String[] args){
              MyFrame f = new MyFrame(); //open the inital gui interface
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.setVisible(true); //set it visibile
          }
      }
  
      //this is the initial gui screen, presenting user with options for which action they would like to take
      //al actions for the gui are commenced here
      class MyFrame extends JFrame{
  
          public MyFrame(){
  
              buttonPanel1 p = new buttonPanel1(); // add the buttons for this frame
              add(p);
  
              setSize(800,600);
              setTitle("Travel Console");
              setLocationRelativeTo(null);
          }
      }
  
      class buttonPanel1 extends JPanel{
          public buttonPanel1(){
              //create buttons
              JButton addItem = new JButton("Add an Item");
              JButton deleteItem = new JButton("Delete an item");
              JButton listItem = new JButton("List items");
              JButton editItem = new JButton("Edit an item");
              JButton bookFlight = new JButton("Book a flight");
              JButton save = new JButton("Save data");
              JButton load = new JButton("Load data");
              JButton exit = new JButton("Exit");
  
              //set layout manager for button panel
              setLayout(new GridLayout(8,1,1,5));
  
              //create buttons
              add(addItem);
              add(deleteItem);
              add(listItem);
              add(editItem);
              add(bookFlight);
              add(load);
              add(save);
              add(exit);
  
              addItemListener addList = new addItemListener();
              addItem.addActionListener(addList);
              exitListener exitList = new exitListener();
              exit.addActionListener(exitList);
          }
  
      }
  
      //listener classes for the inital gui page. each button has its own actionlistener which launches the selected option
      class addItemListener implements ActionListener{
          public void actionPerformed(ActionEvent event){ //launch add item
              addItemFrame addItem = new addItemFrame();
              addItem.setDefaultCloseOperation(addItemFrame.DISPOSE_ON_CLOSE);
              addItem.setVisible(true);
          }
  
      }
  class addItemFrame extends JFrame{
      public addItemFrame(){
          addItemButtonPanel b = new addItemButtonPanel(); // add the buttons for this frame
          add(b);
  
          setSize(800,500);
          setTitle("Add an Item");
          setLocationRelativeTo(null);
      }
  }
  //part of addItemFrame class
  class addItemButtonPanel extends JPanel{
      public addItemButtonPanel(){
          JLabel selectItem = new JLabel("Select which item you would like to add:");
          JButton newCustomer = new JButton("Customer");
          JButton newflight = new JButton("Flight");
          JButton newMovie = new JButton("Movie");
          JButton goBack = new JButton("Return to main menu");
  
          setLayout(new GridLayout(5,1,1,5));
  
          add(selectItem);
          add(newCustomer);
          add(newflight);
          add(newMovie);
          add(goBack);
  
          goBackListener gbList = new goBackListener();
          goBack.addActionListener(gbList);
      }
  }
  
  //listener classes for the addItemFrame
  class goBackListener implements ActionListener{
      public void actionPerformed(ActionEvent event){
          addItemFrame.dispose();
      }
  
  }
  </code></pre>
  
  <p>The problem I am having is with the last class listed <code>goBackListener</code>, which effectively just closes the current window so the main menu screen is displayed again. I need a static reference to <code>addItemFrame</code> created in the <code>addItemListener</code> class. But changing it to static is an invalid modifier?</p>
  
  <p>How can I fix it?</p>