2

I am having a simple question, but somewhat questionable. This is the situation.

In my application, you can open new windows by clicking the new button. When you click the "X" (close) button, it will first ask whether you want to save your work. If no, it will use system.exit(0) to exit. The case is, this closes all the open "new" windows. I want to close only the window which user selected the option "no save". How can I do this? Please help!

11
  • 3
    Don't use System.exit(...) if you don't want to kill the JVM. Do tell us more details if you still need our help. What kind of "windows" are you displaying? JDialogs (this would probably be a good thing to do)? JFrames? How does your code record the state of the windowing object -- whether it is to be saved or not? Commented Jun 5, 2012 at 17:43
  • 2
    stackoverflow.com/questions/258099/… Commented Jun 5, 2012 at 17:44
  • @HovercraftFullOfEels: Thanks for the replies. Much appreciated. JDialog is not a good choice. It is a text editor, like notepad. Uses "new" to open new "windows". It uses a boolean to check whether the text writing place is updated or not. This boolean is controlled by a document filter. I can use JFrame.Dispose_On_Close or something like that to close the window, but the case is, when the user select "no save" option, the window has to be closed. Commented Jun 5, 2012 at 17:56
  • 2
    See The Use of Multiple JFrames, Good/Bad Practice? Commented Jun 5, 2012 at 17:57
  • 4
    "It is a text editor, like notepad. Uses "new" to open new "windows"." Alternatives (all mentioned/linked from the above answer): 1) JTabbedPane 2) JDesktopPane/JInternalFrames 3) CardLayout 4) .. Commented Jun 5, 2012 at 17:59

4 Answers 4

3

System.exit(0) will shut down the JVM currently running your application, and is almost certainly not what you want to do here.

You almost certainly want to do this using a more idiomatic way in Swing.

Sign up to request clarification or add additional context in comments.

Comments

3

For simply close the opened JFrame without exit the application you have to specify what to do on close:

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

For advance control on window closing events, you have to use WindowListener. See here for the official tutorials.

1 Comment

I am afraid, but I think no. I am using window listener to identify whether the file is saved or not, before it is closed.
1

It is a text editor, like notepad. Uses "new" to open new "windows"."

Alternatives (all mentioned in or linked from this answer to The Use of Multiple JFrames, Good/Bad Practice?):

  1. JTabbedPane
  2. JDesktopPane containing JInternalFrame instances
  3. CardLayout
  4. (see linked answer..)

1 Comment

You're welcome. :) Glad to guide you back to the 'bright' side of The Force. ;)
1

WindowUtilities and WindowTiler in TUS are a solution for this problem

http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/ui/util/

1 Comment

While this may be a viable option, it seems a little excessive to include an entire library in the project for a fairly simple problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.