Ernestas Gruodis
Ernestas Gruodis

Reputation: 8787

How to set to null JDialog after it was setVisible(false)?

I have created JDialog and set it setUndecorated(true) - it has no default "Close" button. This JDialog class is created every time when some button on main JFrame is pressed. This JDialog dialog contains button "Cancel" which has MouseListener:

...
public void mouseClicked(MouseEvent e) {
    setVisible(false);
    // How to set this JDialog class to null - release the resources?
}
...

So I want set this JDialog instance to null - because every time button pressed on JFrame creates new JDialog instance - and that is using the resources. How can I resolve this issue?

EDIT

I found that if I each time create new JDialog on mouse click and then set it visible - JDialog window appears not well validated for some milliseconds, after that is OK. So I think more efficient is to create new JDialog instance, and then each time set it setVisible(true) or setVisible(false) when mouse clicked on some JFrame/JDialog buttons.

Upvotes: 2

Views: 506

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

You can call dispose() on the JDialog to free the resource although I've heard that memory leaks can still occur, especially with regards to soft and strong references.


Note that this statement in your question is confusing:

So I want set this JDialog class to null

You cannot set a class to null, only an instance variable.

Upvotes: 4

Related Questions