-1

So when I click on X button the whole program closes but i only want to close the frame which I want to close i am using Net beans. I can't figure it out Google says to add closing event but as i see the X button is already there. What to do?

camickr
  • 321,443
  • 19
  • 166
  • 288
user3551620
  • 169
  • 1
  • 5
  • 17

2 Answers2

3

I am creating a program for inventory management where i have 3-4 frames

An application should only have a single JFrame.

If you need child windows then use a JDialog. When you close a dialog is does not close the main JFrame.

However, for information regarding a JFrame check out the setDefaultCloseOperation(...) method. The default is to "hide" the frame. So if your application is exiting then that means you are probably using the System.exit(...) method somewhere in your code which you should not be doing.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    I have already created frames the program is almost done just 2 things are left closing other frame without closing the program and one other thing. – user3551620 Apr 20 '14 at 04:50
  • 1
    I am not using System.exit anywhere. I am using the X button on the frame which was already there not programmed by me. – user3551620 Apr 20 '14 at 04:52
  • @user3551620, so a JDialog is the same as a JFrame except for the class name. It will take a minute to change the code. There is no time like the present to learn how to design an application properly. This is a small change. – camickr Apr 20 '14 at 04:53
  • 1
    Umm but i want to close the frame not the program. – user3551620 Apr 20 '14 at 04:54
  • @user3551620, `I am not using System.exit anywhere`, did you read the API for the method I mentioned? Does the generated code use that method? `Umm but i want to close the frame not the program` - which is why you should use a JDialog and NOT a JFrame. The default behaviour is to close the dialog when you click on the "X" button. – camickr Apr 20 '14 at 04:56
1

Found in the properties of the Jframe which i want to close defaultCloseOperation was set to EXIT_ON_CLOSE i set it to Dispose.

user3551620
  • 169
  • 1
  • 5
  • 17
  • As I suggested in my answer. This is still not the proper solution. If you don't believe me then check out: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice) for more information. – camickr Apr 20 '14 at 05:18
  • @camickr I know its not the proper way to it but thanks anyway :) – user3551620 Apr 20 '14 at 05:29