0

I have already looked at many other similar questions, but no one has answered to my needs. I need to actually x out of the window itself once an action listener within has been used to open the next window. Below is a portion of my code. the reference to "GUITUI" is a similar section of code in another class. I need to actually exit the window once GUITUI has been called, not just use Frame.dispose() or a similar function to clear the contents of the window.

JPanel Race = new JPanel(new GridLayout(5,6,20,15));
getContentPane().add(Race);
setTitle("Choose a Race.");
setSize(500,200);
setResizable(false);
JButton Race1 = new JButton("Human");
Race.add(Race1);
Race1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
       try {
            GUITUI a = new GUITUI(1);
            a.setVisible(true);
        } catch (IOException e) {
        e.printStackTrace();
        }
}});
  • 1
    `dispose()` _will_ actually dispose the window, no knowledge of "clearing the contents". – Mordechai Apr 30 '14 at 23:41
  • 2
    `dispose()` would work fine for getting rid of the window, but your comment about "clear the contents" is not clear to me. Note that your program design, one of flinging multiple windows at the user, is a common newbie design, and one you will want to avoid in the future as it can be very annoying from a user's point of view. Much better to swap GUI content via a CardLayout or something similar. – Hovercraft Full Of Eels Apr 30 '14 at 23:47
  • From the JavaDocs for [`JFrame#dispose`](http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#dispose()) *"Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable."*. Make sure that the `defaultCloseOperation` is appropriately as well. Also agree with HovercraftFullEels comment. This sounds like a job for `CardLayout` – MadProgrammer May 01 '14 at 00:16
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 01 '14 at 03:14

0 Answers0