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!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • 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? – Hovercraft Full Of Eels Jun 05 '12 at 17:43
  • 2
    http://stackoverflow.com/questions/258099/how-to-close-a-java-swing-application-from-the-code – timaschew Jun 05 '12 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. – PeakGen Jun 05 '12 at 17:56
  • 2
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Jun 05 '12 at 17:57
  • @Sepala: nothing you've stated above would dissuade me from using a JDialog for the dependent windows, but not the main application window. Also, and again, if the answers below do not completely answer your question, please tell us the important details as requested above, and anything else we need to know to be able to understand your problem. – Hovercraft Full Of Eels Jun 05 '12 at 17:58
  • 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`/`JInternalFrame`s 3) `CardLayout` 4) .. – Andrew Thompson Jun 05 '12 at 17:59
  • I appreciate the replies guys :). To be honest, I take the JTabbedPane solution for the next version of this software. Right now, I cant go for such a big deal. – PeakGen Jun 05 '12 at 18:10
  • *"Right now, I cant go for such a big deal."* (Chuckle) You don't yet appreciate how 'big a deal' it will be to get a multi-frame app. working in a reasonable, sensible way. ;) You could start converting it to use a tabbed pane now, & be finished sooner. – Andrew Thompson Jun 05 '12 at 18:14
  • I am glad to get advises from you all. To be honest, it is an fortunate and gift that a new software engineer can have. :) – PeakGen Jun 05 '12 at 18:54
  • @AndrewThompson: please post your answer which has 4 "nice comment" votes, as an "Answer" so I can make it as my selected answer. – PeakGen Jun 06 '12 at 11:03
  • Comments formed into an answer. :) – Andrew Thompson Jun 06 '12 at 11:08

4 Answers4

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.

Community
  • 1
  • 1
Paul Sanwald
  • 10,899
  • 6
  • 44
  • 59
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.

  • 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. – PeakGen Jun 05 '12 at 17:51
1

WindowUtilities and WindowTiler in TUS are a solution for this problem

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

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • 4
    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 – AndyPerfect Jun 05 '12 at 17:47
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..)
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433