Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By jim829

Thread: Window closing event

  1. #1
    Andrew_3ds is offline Member
    Join Date
    Sep 2013
    Posts
    13
    Rep Power
    0

    Default Window closing event

    I have a WindowClosing(WindowEvent e) method, but when I close my window it isn't doing anything inside the method. I am making a launcher and I want to make it so when the actual game window is closed it makes the launcher window visible again.

    Java Code:
    public void windowClosing(WindowEvent e) { 
    		this.jf.setVisible(true);
    	}

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Window closing event

    Are you implementing the appropriate listener and adding it to the window. Check out How to Write Window Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners) for some help. You can also submit a Short, Self Contained, Correct Example which demonstrates the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Andrew_3ds is offline Member
    Join Date
    Sep 2013
    Posts
    13
    Rep Power
    0

    Default Re: Window closing event

    Quote Originally Posted by jim829 View Post
    Are you implementing the appropriate listener and adding it to the window. Check out How to Write Window Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners) for some help. You can also submit a Short, Self Contained, Correct Example which demonstrates the problem.

    Regards,
    Jim
    Thank you I didn't know I needed to implement it. One problem though: it won't let me set a JFrame visible from another class. It is saying that it can't make a static reference to a non-static method, but WindowClosing isn't static.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Window closing event

    Show the code that is causing that exception.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Andrew_3ds is offline Member
    Join Date
    Sep 2013
    Posts
    13
    Rep Power
    0

    Default Re: Window closing event

    Quote Originally Posted by Tolls View Post
    Show the code that is causing that exception.
    I have it so when the play button is hit it the launcher window goes invisible.

    Java Code:
    play = new JButton(""); { 
    			int x1, y1;
    			x1 = (WIDTH / 2) - (btnWidth / 2);
    			y1 = 90 - (btnHeight / 2);
    			play.setBounds(x1, y1, btnWidth, btnHeight);
    			
    			ImageIcon p = new ImageIcon("res/Play.png");
    			play.setIcon(p);
    			
    			panel.add(play);
    			play.setFont(new Font("Colonna MT", Font.BOLD, 15));
    		}
    		{play.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				new Game(GAME_WIDTH, GAME_HEIGHT, "Xenith");
    				setVisible(false);
    			}
    		});}
    Game object is the game window. I want to make it so when I close the game window it make the launcher window visible again. But I can't do Display.setVisible(true); [Display class is the launcher window] because that is static, and I can't do a static command to setVisible, but I also can't create a new Display object because I have the constructor set up so when I create a new Display object it just creates the window
    Java Code:
    public Display(int width, int height, String title) {
    		setSize(width, height);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setLocationRelativeTo(null);
    		setResizable(false);
    		setTitle(title);
    
    		panelActions();
    		
    		setVisible(true);
    		
    		draw(getGraphics());
    	}
    This is the code when I close the window on the game object.

    Java Code:
    @Override
    public void windowClosing(WindowEvent e) { 
    	Display obj = new Display(Display.WIDTH, Display.HEIGHT, "Xenith launcher");
    }
    I am currently just having it create a new Display object, but I don't want it to create more than one Display instance.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Window closing event

    So create a single Display window, and make that accessible by adding that as a constructor parameter to your game window (or panel). The bit that has the windowClosing method.

    ETA: Actually, make the Display class implement the WindowListener and register it as a listener on the game window. Then it can handle making itself visible when the game window closes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Andrew_3ds is offline Member
    Join Date
    Sep 2013
    Posts
    13
    Rep Power
    0

    Default Re: Window closing event

    Quote Originally Posted by Tolls View Post
    So create a single Display window, and make that accessible by adding that as a constructor parameter to your game window (or panel). The bit that has the windowClosing method.

    ETA: Actually, make the Display class implement the WindowListener and register it as a listener on the game window. Then it can handle making itself visible when the game window closes.
    Do you mean add it as a addWindowListener(); ? because if I did that I would still have to create a display object

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Window closing event

    And?
    Not sure I see the problem.

    Don't get rid of the Display window, simply make it invisible.
    When the game window closes make it visible.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Andrew_3ds is offline Member
    Join Date
    Sep 2013
    Posts
    13
    Rep Power
    0

    Default Re: Window closing event

    Quote Originally Posted by Tolls View Post
    And?
    Not sure I see the problem.

    Don't get rid of the Display window, simply make it invisible.
    When the game window closes make it visible.
    It does setVisible when I open the game window. I have my constructor set up so when I make a new Display object it creates a new JFrame. I can't do Display.setVisible from another class because that is static and setVisible isn't; so I can't just do Display obj = new Display(), and then do obj.setVisible because when I create a new Display object it would create a JFrame. I don't want it to create another Display object when the game window is closed, I just want to know if there is a non-static way to access setVisible without creating a new object.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Window closing event

    I recommend you show a small example of the problem, Get rid of all the game stuff and just have your two windows and your window listener. Have just enough code that demonstrates the problem and we can compile and run.

    Regards,
    Jim
    Tolls likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Window closing event

    Quote Originally Posted by Andrew_3ds View Post
    It does setVisible when I open the game window. I have my constructor set up so when I make a new Display object it creates a new JFrame. I can't do Display.setVisible from another class because that is static and setVisible isn't; so I can't just do Display obj = new Display(), and then do obj.setVisible because when I create a new Display object it would create a JFrame. I don't want it to create another Display object when the game window is closed, I just want to know if there is a non-static way to access setVisible without creating a new object.
    This is pseudo-ish code of a basic 2 frame system.

    The code for whatever action (presumably an action listener on a button) would involve something like this:
    Java Code:
    add action listener to whatever button opens the game frame.
        That action listener will include - 
            the creation of the game frame, 
            the adding of "this" to the game frame as a window listener (since Display implements window listener),
            the hiding of the display frame,
            the showing of the game frame.
    The code for the windowClosing method would involve:
    Java Code:
    show the display frame.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Andrew_3ds is offline Member
    Join Date
    Sep 2013
    Posts
    13
    Rep Power
    0

    Default Re: Window closing event

    Quote Originally Posted by Tolls View Post
    This is pseudo-ish code of a basic 2 frame system.

    The code for whatever action (presumably an action listener on a button) would involve something like this:
    Java Code:
    add action listener to whatever button opens the game frame.
        That action listener will include - 
            the creation of the game frame, 
            the adding of "this" to the game frame as a window listener (since Display implements window listener),
            the hiding of the display frame,
            the showing of the game frame.
    The code for the windowClosing method would involve:
    Java Code:
    show the display frame.
    Alright here is my code
    Java Code:
    {play.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent ae) {
    				Game obj = new Game(GAME_WIDTH, GAME_HEIGHT, "Xenith");
    				addWindowListener(obj);
    				setVisible(false);
    				obj.setVisible(true);
    			}
    		});}
    But when I do setVisible in the window closing event in the Game obj it doesn't do anything. I also tried putting it in the windowClosing event in the Display object but it also didn't do anything

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Window closing event

    The Display is the window listener and is added to the Game when it is created.
    You've got it backwards there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    Andrew_3ds is offline Member
    Join Date
    Sep 2013
    Posts
    13
    Rep Power
    0

    Default Re: Window closing event

    Quote Originally Posted by Tolls View Post
    The Display is the window listener and is added to the Game when it is created.
    You've got it backwards there.
    Okay, I got it figured out. Thank you everyone for helping!

Similar Threads

  1. Closing secondary window from button in primary window
    By KoncentriK in forum New To Java
    Replies: 4
    Last Post: 02-26-2012, 02:24 AM
  2. Replies: 1
    Last Post: 07-25-2011, 11:24 AM
  3. Applying action event on window closing?
    By asifzbaig in forum AWT / Swing
    Replies: 2
    Last Post: 06-13-2011, 08:25 PM
  4. Brand new to java ... window is closing
    By drw4d in forum New To Java
    Replies: 5
    Last Post: 03-20-2011, 06:48 PM
  5. how can we call Logout servlet by closing window
    By nagesh1811 in forum Java Servlet
    Replies: 2
    Last Post: 07-11-2008, 08:41 AM

Posting Permissions

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