Results 1 to 5 of 5
  1. #1
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default Closing multiple windows

    Hi,
    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.

    Help appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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:

    getWindows
    getOwnedWindows
    getOwnerlessWindows

    They can probably help you out.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    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.

    db

  4. #4
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    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;
    Last edited by jonytek; 05-24-2011 at 07:16 AM.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    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.

    db

Similar Threads

  1. trying to use multiple JFrame Windows
    By tidus1103 in forum AWT / Swing
    Replies: 0
    Last Post: 03-08-2011, 03:40 PM
  2. multiple windows coming up
    By Kyle227 in forum New To Java
    Replies: 8
    Last Post: 04-27-2010, 01:14 AM
  3. multiple windows and eventlisteners
    By kmyr in forum New To Java
    Replies: 3
    Last Post: 01-10-2010, 01:26 PM
  4. Closing an Editor
    By janpol1 in forum Eclipse
    Replies: 0
    Last Post: 02-14-2009, 04:00 PM
  5. Opeing multiple pdf files in different acrobat reader windows
    By shweta.ahuja in forum Web Frameworks
    Replies: 2
    Last Post: 05-07-2008, 12:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •