# Java Terminating Program

by muna580
Tags: java, program, terminating
 P: n/a 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?
 Emeritus Sci Advisor PF Gold P: 10,427 System.exit(0); - Warren
 P: n/a 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?
 P: 223 Java Terminating Program 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.
 Emeritus Sci Advisor PF Gold P: 16,091 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.
P: n/a
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
 GUI Interface.zip (15.8 KB, 19 views)
 P: 223 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.
P: n/a
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

 public void windowClosing(WindowEvent e) { try { int option = JOptionPane.showConfirmDialog(null, "Would you like to close the application?", "Select an Option", JOptionPane.YES_NO_OPTION); BasicFile.bf.close(); if(option == 0) System.out.println("Bye"); } catch(IOException ie) { JOptionPane.showMessageDialog(null,"Error Occured"); } }
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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f is the object of the myJFrame class.
 P: 223 The last part of your post seems to answer your own question. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 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.
 P: n/a Edit: 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?  this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { try { int option = JOptionPane.showConfirmDialog(null, "Would you like to close the application?", "Select an Option", JOptionPane.YES_NO_OPTION); if(option == 0) { BasicFile.bf.close(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } catch(IOException ie) { JOptionPane.showMessageDialog(null,"Error Occured"); } } } );
 P: 223 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.
 P: 223 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.

 Related Discussions Programming & Computer Science 6 Engineering, Comp Sci, & Technology Homework 0 Programming & Computer Science 18 Computing & Technology 11 Computing & Technology 5