Suthar
Suthar

Reputation: 91

how I can dispose a frame created through New JFrame in java using NetBeans?

File is created using Project → New(Right Click) → JFrame Form

I set title as XYZ and I am trying to dispose this frame using a button on the frame using XYZ.dispose();

But actually not working

Upvotes: 0

Views: 614

Answers (1)

Kartik Gautam
Kartik Gautam

Reputation: 128

When you create a new JFrame this way (the way you have mentioned), then frame.dispose() will not work as non-static method can't be referenced from a static context.

As you are closing the main frame not any internal frame,

For particularly disposing that frame:

this.dispose();

You can also do the exiting if you want, get the job done via:

System.exit(0);

For specifically disposing that frame, rather add one more instruction to your closing function with the code instruction:

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
/*an import required for this--> import javax.swing.WindowConstants;*/

This closes the main frame (not the same as disposing of internal frames!).

Upvotes: 1

Related Questions