title: How do I close a JDialog with the behavior I want?
body: <p>I am just beginning to learn Java so please be gentle with me. I find the default close behaviors of frames and dialogs confusing.</p>
  
  <p>I am trying to create a simple login dialog box using Swing. I have taken a <code>JDialog</code> for the dialog box. I just want that when the user clicks the <strong>Login</strong> button, after doing the authentication stuff, I want the login window to close but another window to open.</p>
  
  <p>If, however, the user clicks the <strong>Close</strong> button, the login box should close but also the application must end.</p>
  
  <p>I know how to write action listeners and the authentication code. I just need help with understanding what close behavior I must implement.</p>
  
  <p>Here is my code so far (only the relevant snippets). More specific questions are in the comments near the action listeners.</p>
  
  <pre><code>package practice.bookyard.client;
  
  import javax.swing.SwingUtilities;
  
  public class Program {
  
      public static void main(String[] args) {
  
          SwingUtilities.invokeLater(new LoginDialogEventLoop());     
      }
  
  }
  
  
  public class LoginDialogEventLoop implements Runnable {
  
      @Override
      public void run() {
  
          // Create and set title
          JDialog dialog = new LoginDialog("Bookyard Login");
  
          // Set default close behavior
          dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  
          // Set size
          dialog.setSize(400, 300);
          // dialog.setResizable(false);
  
          // Set position on the screen
          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
          dialog.setLocation((int)screenSize.getWidth() / 2, (int)screenSize.getHeight() / 2);
          dialog.setLocationRelativeTo(null);
  
          dialog.setVisible(true);
      }
  
  }
  
  public class LoginDialog extends JDialog {
  
      private static final long serialVersionUID = 1L;
  
      protected JLabel lblTopSpace = null;
      protected JPanel loginPanel = null;
      protected JPanel statusPanel = null;
  
      public LoginDialog(String title) {
  
          super((Dialog)null);
  
          this.setTitle(title);
  
          Initialize();
      }
  
      protected void Initialize() {
  
          // DONE: add a panel and add controls to the panel
          ...
      }
  }
  
  
  
  
  
  public class LoginPanel extends JPanel {
  
      private static final long serialVersionUID = 1L;
  
      protected JLabel lblUserName;
      protected JLabel lblPassword;
      protected JTextField txtUserName;
      protected JPasswordField txtPassword;
      protected JButton btnLogin;
      protected JButton btnCancel;
  
      public LoginPanel() {
          super();
  
          this.Initialize();
      }
  
      protected void Initialize() {
  
          this.setLayout(new GridBagLayout());
          GridBagConstraints constraints = new GridBagConstraints();
  
          // Create controls
          lblUserName = new JLabel("User Name: ");
          lblPassword = new JLabel("Password: ");
          txtUserName = new JTextField(15);
          txtPassword = new JPasswordField(15);
          txtPassword.setEchoChar('*');
          btnLogin = new JButton("Login");
          btnCancel = new JButton("Cancel");
  
          // Set them on the panel using the constraints
          // 3 rows, 3 columns
          // DONE ALREADY
          ...
  
  
  
          btnLogin.addActionListener(new ActionListener() {
  
              @Override
              public void actionPerformed(ActionEvent e) {
                  // authenticate user: I know how to do this
  
                  // how do I close the login JDialog
                  // of course, I will need a reference to the dialog here
                  // which is not a big deal. I can have that be passed
                  // in via a constructor.
                  // So, supposing this 'dialog' variable represented the
                  // login dialog
                  JDialog dialog = new JDialog((Dialog)null);
  
                  // What's the method to close this window?
                  // I believe it is dispose()
                  // But will calling the dispose method also
                  // exit the main thread since this is the 
                  // only window in the application?
                  // Also, should I be launching the new window from here?
                  // or from within the main() of the application?
                  // Is there a way this dialog can communicate to its
                  // creator what the dialog result was -- Login or Cancel?
                  dialog.dispose();
  
                  JFrame frame = new JFrame("New Window");
                  frame.setSize(400,  400);
                  frame.setVisible(true);
  
  
              }
          });
  
          btnCancel.addActionListener(new ActionListener() {
  
              @Override
              public void actionPerformed(ActionEvent e) {
                  // So, supposing this 'dialog' variable represented the
                  // login dialog
                  JDialog dialog = new JDialog((Dialog)null);
  
                  // This one is a bit easier
                  // But since this is a JDialog and not a JFrame
                  // I cannot set the default close behavior
                  // to EXIT_ON_DISPOSE. I can only set it to:
                  // HIDE ON DISPOSE
                  // CLOSE ON DISPOSE
                  // DO NOTHING ON DISPOSE
                  // And since I have set it to CLOSE ON DISPOSE in Program.java
                  // just calling dispose() on this will keep the application
                  // still alive, right?
                  dialog.dispose();
              }
          });
  
      }
  }
  </code></pre>