views:

19

answers:

1

This is my code I have developed. This is the main program which holds and executes each external JFrame for my Game. chooseGender is an external program which is nothing but a JFrame and its components.

My goal for this is when chooseGender executes, it has 2 buttons for options (male, female) when the user picks one, an actionListener would set the frame to setVisible(false) and then have a WindowClosing event open the next JFrame, (chooseRace). This would happen for several more frames but these 2 are for learning purposes. I appreciate the help in advance. :)

So my question is, how would I go about adding a WindowListener to chooseGender in this program so I can close it and open the next one?

package javagame;

import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;

public class Main implements WindowListener {


    public static void main(String[] args) {

             EventQueue.invokeLater(new Runnable() {

            public void run() {
                new chooseGender().setVisible(true);
            }
        });

          EventQueue.invokeLater(new Runnable() {

            public void run() {
                new chooseRace().setVisible(false);
            }
        }); 
    }
+1  A: 

An easy way to implement this may be just using modal JDialogs.

The code would be similar to the following:

main {

    new chooseGender().setVisible(true);
    new chooseRace().setVisible(true);
    new chooseAge...

}

You would want to implement a WidowListener similar to the following:

public class OpenNewWindowWindowListener extends WindowAdapter {
    public void windowClosing(WindowEvent e){
        // in here open the next window.
    }
}

And add that window listener to the correct frame:

// In the constructor for the JFrame
addWindowListener(new OpenNewWindowListener());

And, each of those classes would extend JDialog and, in their constructors use setModal(true).

jjnguy
Hold on, I'll have to research that. I haven't gotten that far into Java yet.
Nick Gibson
Er, They would still be JFrames though correct, and would still execute the way* I would like?
Nick Gibson
@Nick, you would need to make them `JDialogs`. `JDialogs` are very similar to JFrames.
jjnguy
Thats not what I am wanting to do though. I'm trying to use them as JFrames. Thank you for the suggestion, it has made me look into JDialogs and I will implement them in the future, but I really really need to learn JFrames. This is one of the challenges I am facing right now and would like to learn how to do this correctly.
Nick Gibson
Which leads me back to my original question of, where would I add my windowListener in this program to sense if chooseGender has closed?
Nick Gibson
@Nick, I will add some code in a bit. Lunch time right now.
jjnguy
@Nick, ok check out my edit. Also, an upvote for my trouble couldn't hurt?
jjnguy