Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Terminating Program

  1. Dec 13, 2006 #1
    I have a window here and I want it to close the whole program when the user clicks on File>>>Close. I just need to know waht to put inside the method to close the program. I mean, how do you terminate the program in java?
  2. jcsd
  3. Dec 13, 2006 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member


    - Warren
  4. Dec 13, 2006 #3
    Also, my professor says this

    Create an anonymous WindowListener Object such that when you click the close button on your frame, a child frame (JOptionPane frame) appears. Remember to import the class WindowAdapter, the class WindowEvent.

    My question is, basically I am going to make a JOpitonPane menu pop up asking the user if you want to close or not. But how do I like get a boolean vaule from the user using JOptionPane in order to find out if the user really wants to close the window or not.

    Also, what should I type in, to safely close and fame and a file that was open in that frame?
    Last edited by a moderator: Dec 13, 2006
  5. Dec 13, 2006 #4
    Your question seems to be a candidate for your own endeavours only. The solution will require that you understand what you're doing, not that we show you some code. You should be able to receive an integer from the "JOpitonPane" function, that you can respond appropriately to. If they click yes, call System.exit. If not, proceed accordingly.

    As to safely closing the application, this pertains to what you are accomplishing. If you have a write stream open, close it. If you have a read stream open, it's advised to do the same, but not mandatory. If a user has modified something where the changes have not been made effective, do whatever is necessary to respond as you require.

    Test a few things. See if you can get it working. If you're having difficulties, post your code. Someone will be able to suggest a less ambiguous solution to your ambiguous problem.
  6. Dec 13, 2006 #5


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    And, just for the record, you should avoid calling System.exit when possible. Normal termination is when all threads have terminated. (The parent thread terminates when main finishes)

    System.exit is a somewhat unilateral solution. It may suit your purposes now, but it can create problems in the future. For example, you might write a new application that, for some reason, wants to invoke an instance of this application... and if this application called System.exit, it would (inappropriately) terminate your new application.
  7. Dec 13, 2006 #6
    How would I close the stream if the stream that is open is in another class but the method I am coding to close application properly is a different class?

    I uploaded my coding. There are 8 classes. The "main/driver" class is the myWindow class.

    Take a look at the myJFrame class. Go to line 29-36, which is the windowClosing method. Right now, I just put in a code over there just for testing reasons. But it is not working properly. First of all, how do I make it so, if the user closes the window, it pops up a YES/NO option if they want to close it. If they say yes, then the application is 'safely' closed. Also, when we mention the word 'safely' I am guessing the fact that the stream that is open has to be closed too. But the stream that is open is in the BasicFile class. How do I carry out this process?

    Now take a look at the Actions class. Go to line 22-29. Basically, I want to do that same process with closing the application.

    I have uploaded the file to this post.

    Attached Files:

  8. Dec 13, 2006 #7
    If you wish to access a method of a class that does not belong to the class you're currently in, I see a couple options available to you...

    If you require a certain instance of the other class, in order to call the method (we'll call the instance of the desired class's method the "active instance"), then you could store a pointer to the active instance, in the class you wish to call it from. This handle will represent the same handle that could be used outside the current class, and therefore you could use the other class's methods just as you would with a regular handle.

    Instead of storing the handle, if the depth of the method you expect to call the other method from, is quite shallow, you could merely pass through the instance of the class to the function. Then do the same as what was previously mentioned.

    If the method you wish to call is independant from the program's current state, or, in other words, does not require that its variables are the same as the "active" instance of the class, then you could call this method independantly from this active instance. Start a new instance. Call the method. Or make the entire class inherit the other class (class inheritance), so it has direct access to this method internally.
    Last edited: Dec 13, 2006
  9. Dec 13, 2006 #8
    I don't really understand what you just said, but this is what I did.

    In the BasicFile class, I made a new variable called "static BufferedReader bf". This bf is going to be used in the method called getContent() of the BasicFile class.

    Then I just went to the myJFrame class and put this in

    Now I am having only one problem. I need help with closing the application. I mean, like for example, if the user closes the frame by pressing X on the window, and even though I have that method I posted above, it will still close the frame no matter what option the user choose.

    Also, when do I use this statement?


    f is the object of the myJFrame class.
  10. Dec 13, 2006 #9
    The last part of your post seems to answer your own question.
    Code (Text):
    That should set the default close operation (even if they click 'X' on the window), to whatever is specified. You would put this code into the constructor of your frame.

    It has become irrelevant to clarify my last post, since you seem to have that under control now. But while I'm mentioning it, it looks like you did what I had suggested as a third solution. I believe.

    But either you didn't do what I think you did, or your highlighted line won't do exactly what you expect it to.
    Last edited: Dec 13, 2006
  11. Dec 13, 2006 #10

    I take back what I said. Umm there is a problem. Look at my code below. After adding BasicFile.bf.close();, I get an error. Like every time I press the X to close the window and then press YES to officially close the window, the DOS screen shows some errors and the frame doesn't' close.

    Also, by putting in setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);, the program will close ALL the frames that are open. What I mean is, lets say I have the current frame open. And then if I have antoher frame open (BROWSER), and I close the browser, it will close both the parent frame, and the broswer. How do I make it only close the browser?

    Code (Text):
                new WindowAdapter()
                    public void windowClosing(WindowEvent e)
                            int option = JOptionPane.showConfirmDialog(null, "Would you like to close the application?",
                            "Select an Option", JOptionPane.YES_NO_OPTION);
                            if(option == 0)
                        catch(IOException ie)
                            JOptionPane.showMessageDialog(null,"Error Occured");
    Last edited by a moderator: Dec 14, 2006
  12. Dec 14, 2006 #11
    How much understanding do you have of classes and their instances? If you are initiating a separate instance of the "BasicFile" class, one which does not have the correct state of "bf", you won't be closing the same stream that was previously opened. You will be closing a stream that does not yet exist. Unless I misunderstand the design of your code.

    If you don't want to close all of the frames when the 'X' is clicked, set the DefaultCloseOperation to a custom hook. This hook will receive the event. Then it is your duty to delete the currently raised exit event, and send a signal to close the other frame. One problem may be that this hook might be called every time you try to exit the application. So you will have to make a distinction between a complete exit and a partial exit, which would be achieved by looking at the raised event. I have never done anything of this sort in Java. This is deductive reasoning based on my experience with handling GUI events in other toolkits such as wxWidgets. If I'm not completely correct, I should at least be very close.

    I suggest that you google these commands you are using, in hopes of finding documentation and examples.

    I also apologize for any inadequacies or inaccuracies. I haven't had time to look at your code as I'm currently very busy, yet trying my best to help you. I hope that at a later time, if someone does not beat me to it, I can take a thorough look at the problem.
    Last edited: Dec 14, 2006
  13. Dec 15, 2006 #12
    Have you made any progress on this, or found any examples that help? I just found out that I don't have Java on my computer anymore. Suggesting code I can't test would not be a pretty scene.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook