Closing multiple windows
I have created a program that shows a "Game" window. From this window I make multiple windows "Player(s)". I can have multiple "Game" windows. What I need is to find is a way that if I close a "Game" window it closes the associated "Player(s)" windows but doesnt close the other "Game" windows.
I have set up a window listener using the windowClosing(WindowEvent e) method but im not sure exactly what to put in its body.
I assume that the Game and Player windows are JDialogs and that they derive from the Window Class? If so, have you tried using one of the Windows methods that can list the program's windows that are open:
They can probably help you out.
A word of caution: the array returned by getWindows() includes references to windows that are no longer visible or even displayable -- even windows that are no longer in scope. On all current Sun (Oracle?) JVMs a call to System.gc() removes those, but that behavior isn't mandated by the JLS nor the JVM spec. Code calling this method without first collecting garbage should preferably declare the array in a very limited scope so as not to hold on to references to windows that are no longer in use, thus preventing their collection. Alternatively, while looping over the array, any reference to a non-visible window could be set to null.
Ok I just tried the getOwnedWindows() method but it returns 0 while the getOwnerlessWindows() method returns all the windows. This is odd as all windows are created from the first instance of the "Game" window. Therefore the getOwnedWindows() should return a Window containing all windows;
Obviously, you didn't construct those windows with an owner parameter. Also note that among Swing top-level windows, JWindow and JDialog have an owner (which may be null). JFrame cannot.
Creating instances of an object from an instance of another object doesn't automagically establish a relationship between them.