Question:

CompSci Intro Java Question: Closing a JFrame

by Guest56135  |  earlier

0 LIKES UnLike

In programming for our computer science game, we have a JFrame open displaying a panel which opens another panel in another JFrame. However, we can't get the second JFrame to close without closing the initial JFrame.

Setting the second panel invisible doesn't close the JFrame, but only closes the panel. We tried "frame.dispose()" in the first panel, but the problem is that the second JFrame closes immediately, before the second panel even does anything.

Preferably nothing too difficult to manage?

Thanks in advance!

 Tags:

   Report

2 ANSWERS


  1. I'm guessing that the second JFrame gets some input from the user and you need to retrieve that before the user makes the second JFrame go away?  If so, this is what you really need to do:

    1.  Set the second JFrame to DO_NOTHING_ON_CLOSE.  You want to be able to control exactly what happens, and in this case, you want to execute some code BEFORE the JFrame gets disposed.

    2.  Register a WindowListener with your second JFrame.  It's easiest to write a sub-class of WindowAdapter.  You want to provide an implementation for the windowClosing() method, as this gives you the opportunity to intercede before the window closes.

    3.  In your window listener, retrieve the input data and send it back to your original JFrame, or more correctly, to some handler object that is held by your primary application.  The last thing it should do is call dispose() on the second JFrame.  So now, when the user clicks the "X" or presses Ctrl-F4 on your second JFrame, the input data it holds will be sent to the data handler PRIOR to disposing of it and it's contained components.


  2. The only two function calls I remember for what you are trying to do are refToJFrame.hide() which will hide the frame and refToJFrame.dispose() which will remove the frame from memory. It sounds like your problem is that you are calling dispose() too early in your code which is why the 2nd panel can not do anything. You may want to look at containing the dispose() call in a conditional that waits for an action to occur in the second panel.  

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions