1

I'm new to Java desktop application. How do I make the application exit only when there is no frame open? In VB.NET there is an option in the project properties that says "Exist when last form closes" something like that.

Basically I have two frames, Login and MainMenu. The Login shows first, and when the user successfully logged in, the Login will be closed and the MainMenu will open.

kazinix
  • 28,987
  • 33
  • 107
  • 157
  • 2
    Possible duplicate: http://stackoverflow.com/questions/258099/how-to-close-a-java-swing-application-from-the-code – Kazekage Gaara May 21 '12 at 06:11
  • @KazekageGaara not a duplicate, however the answer chosen solved my problems ;) Thanks! – kazinix May 21 '12 at 06:17
  • 1
    Don't use two `JFrame`s , instead either take the approach of [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html), or go with One `Jframe` and [JDialog](http://docs.oracle.com/javase/7/docs/api/javax/swing/JDialog.html) for getting information about the needed stuff. Here is one wondoerful example given by @kleopatra , regarding [How to find an open window in Swing](http://stackoverflow.com/questions/5890649/how-do-i-find-if-a-window-is-opened-on-swing/5891192#5891192) – nIcE cOw May 21 '12 at 06:26
  • @dpp: if problem is solved, accept an answer please or give your own answer and accept it. – Aleksandr Kravets May 21 '12 at 06:27
  • 1
    It is `setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); `! See [this example](http://stackoverflow.com/a/7143398/418556). The JRE only exits after the last frame disappears. +1 to the advice of @nIcEcOw - see also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson May 21 '12 at 06:28
  • @AleksandrKravets It solved my problem, yes. But, I'm not sure it's the best approach. – kazinix May 21 '12 at 06:31
  • @dpp: I'm not sure whether you intended to learn java and frameworks in deep. So for now i can propose look towards MVC pattern and SWING in particular. Controller will help tou to count opened windows and shutdown app when all of them closed(disposed in your case). Or you can create Factory for your windows which will make same thing. – Aleksandr Kravets May 21 '12 at 06:44

3 Answers3

4

You can put all your frames in a List add a window closing event to each of the frame and remove it from the list on window close.If the list contains no element you can exit the application.

Additionally you can use setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Nitin Chhajer
  • 2,299
  • 1
  • 24
  • 35
3

When you create your top-level frame, make sure you tell it what to do when it's closed, using the setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); code snippet.

Basically, in Java, if you close a window, it's just hidden, not closed, so you can re-use it. This tells Java than when this particular window is closed, you intend to exit the application.

Have you had a chance to look at the Java Swing tutorial trails? If you have a moment, browse to http://docs.oracle.com/javase/tutorial/uiswing/ - they have a lot of useful information there that might help you. Some of the Swing techniques are definitely different to what you might be used to from a .NET perspective.

Ewald
  • 5,691
  • 2
  • 27
  • 31
  • what do you mean when you say top level frame? – kazinix May 21 '12 at 06:20
  • 1
    dod you click to the link that Ewald posted, second from the top is [Top-Level Containers](http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html) – mKorbel May 21 '12 at 06:24
  • 1
    I dont think that solves the question. If the top level frame opens new children frame, closing the top level frame still exits the app – Nitin Chhajer May 21 '12 at 06:26
  • That depends on the design, right? If I open my browser, and then close the main window, I expect the child windows to go away by themselves. Perhaps using a modal frame or dialog is a better solution. I've no idea what the OP wanted to do in the first place. – Ewald May 21 '12 at 06:27
  • As for what a top-level frame is - It's primarily the MAIN window of your application, that one window that exists for the lifetime of the application, unless, of course, you have several, which once again, depends on your design. – Ewald May 21 '12 at 06:28
  • @NitinChhajer Actually, that's what happened when I added the EXIT_ON_CLOSE on the Login frame. – kazinix May 21 '12 at 06:29
  • `JFrame.DISPOSE_ON_CLOSE` for multiple frames. – Andrew Thompson May 21 '12 at 06:31
  • Ugh. Beaten by @AndrewThompson again, and it's only Monday. Andrew, where do you get all these great answers? Share! :) – Ewald May 21 '12 at 06:42
2

place this in the JFrame constructor, setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

Now for good GUI app in Java, use the below in main fun

EventQueue.invokeLater(new Runnable() {

   MyFrame f = new MyFrame();

   f.setVisible(true);

}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75