I have a standalone Java application that gets data from a database and displays it in a `JTable`. When the application starts, the user is prompted for a username/password in a `JDialog`. Once the correct credentials are entered, the main `JFrame` containing the data is displayed. On the main `JFrame` I have a logout button that, when clicked, should close the main `JFrame` and redisplay the login `JDialog`. Everything is mostly working except I have found that the main `JFrame` does not go away when the logout button is clicked. Below is a small working example of my code:

**Main.java:**

```
import javax.swing.SwingUtilities;

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MainFrame();
            }
        });
    }
}

```

**MainFrame.java:**

```
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class MainFrame extends JFrame implements ActionListener {
    private JButton button;
    private MyDialog dialog;

    public MainFrame() {
        super("this is the JFrame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dialog = new MyDialog(this);
        button = new JButton("click me to hide this JFrame and display JDialog");
        button.addActionListener(this);
        add(button);
        pack();
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        setVisible(false); // works when changed to dispose();
        dialog.setVisible(true);
    }
}

```

**MyDialog.java:**

```
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class MyDialog extends JDialog implements ActionListener {
    private JFrame parentFrame;
    private JButton button;

    public MyDialog(JFrame parentFrame) {
        super(parentFrame, "this is the JDialog", true);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.parentFrame = parentFrame;
        button = new JButton("click me to hide JDialog and show JFrame");
        button.addActionListener(this);
        add(button);
        pack();
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        setVisible(false);
        parentFrame.setVisible(true);
    }
}

```

In **MainFrame.java** if I change the line that says `setVisible(false)` to `dispose()` then the JFrame goes away when I click the button. My question is, why does this work with `dispose()` and not with `setVisible(false)`? Is there a better way for me to organize my code? I am new to Swing so I apologize if this is a basic question. Thank you.

---

**EDITED 2011-10-19 10:26 PDT**

Thank you everyone for your help. I changed the `JDialog` to have a null parent and now everything works as I wanted.