Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete an object referenced by a dialog?

Tags:

java

swing

I created a dialog with a jpanel inside it, and that jpanel will be still referenced if I get rid of the dialog. I want to destroy that dialog and everything in it when I click a cancel button. How do I delete both the dialog and the jpanel?

like image 207
joseph Avatar asked Mar 15 '10 06:03

joseph


3 Answers

Answering the question(s) you posed in the comment:

Once you have displayed a dialog:

setVisible(true);

you hide it by calling:

setVisible(false);

and then you must call:

dialog.dispose();

to make sure that all native GUI resources the dialog used get freed. Once you have done this, the garbage collector will clean up all of the objects once you no longer have any references to them.

like image 162
David Sykes Avatar answered Oct 31 '22 15:10

David Sykes


  1. If it is a Window and if it is visible

    I. frame.setVisible(false);

    II. frame.dispose();

    II. set reference to null (ex.. frame=null;)

  2. If it is not a Window

    I.set reference to null (ex.. x=null;)

That is all , once object be free GC will free the resources when it is like.

Here are somethings you must understand

*. You as Java programmer can not force Garbage collection in Java; it will only trigger if JVM thinks it needs a garbage collection based on Java heap size.

*. There are methods like System.gc () and Runtime.gc () which is used to send request of Garbage collection to JVM but it’s not guaranteed that garbage collection will happen.

*. If there is no memory space for creating new object in Heap Java Virtual Machine throws OutOfMemoryError or java.lang.OutOfMemoryError heap space

And search about this...

J2SE 5(Java 2 Standard Edition) adds a new feature called Ergonomics . goal of ergonomics is to provide good performance from the JVM with minimum of command line tuning.

like image 5
Lalith J. Avatar answered Oct 31 '22 14:10

Lalith J.


No need to delete the object. The garbage collector will take care of the memory as soon as it is no longer referenced.

like image 4
Raul Agrait Avatar answered Oct 31 '22 13:10

Raul Agrait