title: Java : Swing : Hide frame after button pressed
body: <p>I have a button in a java frame that when pressed it reads a value from a text field and uses that string as a port name attempting to connect to a serial device.</p>
  
  <p>If this connection is successful the method returns true if not it returns false. If it returns true I want the frame to disappear. A series of other frames specifed in other classes will then appear with options to control the serial device.</p>
  
  <p>My problem is: the button is connected to an action listener, when pressed this method is invoked. If I try to use the frame.setVisible(true); method java throws a abstract button error because I'm effectively telling it to disappear the frame containing the button before the button press method has exited. Removing the frame.setVisible(true); allow the program to run correctly however I am left with a lingering connection frame that is no longer any use.</p>
  
  <p>How to I get the frame to disappear upon pressing a the button?</p>
  
  <pre><code>package newimplementation1;
  
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  
  /**
   *
   * @author Zac
   */
  
  public class ConnectionFrame extends JPanel implements ActionListener {
  
  
  private JTextField textField;
  private JFrame frame;
  private JButton connectButton;
  private final static String newline = "\n";
  
  public ConnectionFrame(){
  
      super(new GridBagLayout());
  
      textField = new JTextField(14);
      textField.addActionListener(this);
      textField.setText("/dev/ttyUSB0");
  
      connectButton = new JButton("Connect");
  
      //Add Components to this panel.
      GridBagConstraints c = new GridBagConstraints();
      c.gridwidth = GridBagConstraints.REMAINDER;
  
      c.fill = GridBagConstraints.HORIZONTAL;
      add(textField, c);
  
      c.fill = GridBagConstraints.BOTH;
      c.weightx = 1.0;
      c.weighty = 1.0;
      add(connectButton, c);
  
  
  
      connectButton.addActionListener(new ActionListener() {
  
          public void actionPerformed(ActionEvent e)
          {
  
              boolean success = Main.mySerialTest.initialize(textField.getText());
  
              if (success == false) {System.out.println("Could not connect"); return;}
  
              frame.setVisible(false);  // THIS DOES NOT WORK!!
  
              JTextInputArea myInputArea = new JTextInputArea();
              myInputArea.createAndShowGUI();
  
              System.out.println("Connected");
  
  
          }
      });
  
  }
  
      public void actionPerformed(ActionEvent evt) {
  
              // Unimplemented required for JPanel
  
      }
  
      public void createAndShowGUI() {
  
      //Create and set up the window.
      frame = new JFrame("Serial Port Query");
      frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  
  
      //Add contents to the window.
      frame.add(new ConnectionFrame());
      frame.setLocation(300, 0);
  
  
      //Display the window.
      frame.pack();
      frame.setVisible(true);
  
              frame.addComponentListener(new ComponentAdapter() {
          @Override
          public void componentHidden(ComponentEvent e) {
              System.out.println("Exiting Gracefully");
              Main.mySerialTest.close();
              ((JFrame)(e.getComponent())).dispose();
              System.exit(0);
          }
      });
  
  
  }
  
  }
  </code></pre>