-1

I display two JFrame objects at the end of my program. But when I close JFrames, main() thread does not stop. It waits for me to click on stop button (the red rectangle in the picture). But I want main to stop when I close all the JFrames. Is that possible?

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
padawan
  • 1,295
  • 1
  • 17
  • 45
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 19 '14 at 20:54
  • See also this [demo. of `DISPOSE_ON_CLOSE`](http://stackoverflow.com/a/7143398/418556). But really, see my first comment. – Andrew Thompson May 19 '14 at 20:58
  • 1
    Staggered I can't find a duplicate for this one... – Duncan Jones May 19 '14 at 20:58
  • @AndrewThompson I've read the post you commented. The post refers to two main drawbacks. I have none of them. I am not developing a GUI. So, design is not my main concern. – padawan May 19 '14 at 21:01
  • @Duncan No problem, I could. ;) – Andrew Thompson May 19 '14 at 21:02
  • @AndrewThompson I am just displaying the results of my code. I will take the screenshots and put them in my research paper. Actually, what I am doing is none of your concern. – padawan May 19 '14 at 21:03
  • *"I will take the screenshots.."* Speaking of which: See [How do I create screenshots?](http://meta.stackoverflow.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post) (for tips on making *great* screenshots). – Andrew Thompson May 19 '14 at 21:17
  • @AndrewThompson I am writing the contents of `JFrame` to a `.png` file. Thank you for your guidance. – padawan May 19 '14 at 21:22

2 Answers2

2

I'd suggest writing a window closing listener:

Be careful about cleaning up after yourself. You could just call System.exit(), but perhaps a better solution would do resource cleanup.

The stop button and window listener could call the same method to ensure consistency.

dev2d
  • 4,245
  • 3
  • 31
  • 54
duffymo
  • 305,152
  • 44
  • 369
  • 561
-1

You can set the default close operation when you initialize your frame using this command:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

According to the Javadocs, this will 'Exit the application using the System exit method' when you close the frame. I just tried this, and if you have two JFrames open, closing one will close all frames and stop the main() thread.

shimizu
  • 998
  • 14
  • 20
  • So, I need to write this the very first `JFrame`. Because I don't want `main()` to stop when I close just one of them/ – padawan May 19 '14 at 21:11
  • ..did you actually try using the code linked in my 2nd comment? Changing `DISPOSE_ON_CLOSE` to `EXIT_ON_CLOSE` might have answered that question for you within 60 seconds. – Andrew Thompson May 19 '14 at 21:24
  • The point you seem to be missing here is that there are multiple `JFrame` objects on screen. `EXIT_ON_CLOSE` is the last thing that needs, since closing one frame will close them all. – Andrew Thompson May 19 '14 at 21:26
  • Oh I see, I misunderstood the question. I thought he just wanted everything to close when one frame closed. – shimizu May 19 '14 at 21:53