Dirty dispose?

A

Andreas Thiele

Hi,

I defined a JDialog class ConnectionChooser from which the user can select a
database connection. Invocation should be similar to
JFileChooser.showOpenDialog(). Thus my class contains the following method
which works fine:

public SheetConnection showConnectDialog() {
this.setModal(true);
this.setVisible(true);
this.dispose();
return this.sheetConnection;
}

This method displays the dialog and waits until the user selects a
connection to return an encapsulating object to the caller.

Now this looks somehow dirty to me. I dispose the current object and
afterwards I access one of its member variables to return it to the caller.
Is this technique OK? Is dispose posted to an event queue and thus executed
later (after leaving the method)? Obviously dispose is not executed directly
because in this case I should have received some run time error?

Any hints appreciated :)

Andreas
 
T

Thomas Weidenfeller

Andreas said:
Now this looks somehow dirty to me. I dispose the current object and
afterwards I access one of its member variables to return it to the caller.

dispose() does not dispose, or garbage collect the object as such, it
just releases any native resource which might be associated with the
object. The object as such is still fine. In fact, one could even
display such a window again, after a call to dispose(). The window would
just have to freshly acquire the needed native resources again.

/Thomas
 
J

John C. Bollinger

Andreas said:
I defined a JDialog class ConnectionChooser from which the user can select a
database connection. Invocation should be similar to
JFileChooser.showOpenDialog(). Thus my class contains the following method
which works fine:

public SheetConnection showConnectDialog() {
this.setModal(true);
this.setVisible(true);
this.dispose();
return this.sheetConnection;
}

This method displays the dialog and waits until the user selects a
connection to return an encapsulating object to the caller.

Now this looks somehow dirty to me. I dispose the current object and
afterwards I access one of its member variables to return it to the caller.
Is this technique OK? Is dispose posted to an event queue and thus executed
later (after leaving the method)? Obviously dispose is not executed directly
because in this case I should have received some run time error?

This sort of thing makes me nervous (as apparently it does you, too).
It may work fine, and there may even be good reason to expect it to work
fine, but it seems to be asking for trouble. Why can't the code that
hides the dialog be responsible for invoking dispose()? Or is dispose()
solving a real problem for you at all? Could you just remove it altogether?
 
I

Ian Shef

Hi,

I defined a JDialog class ConnectionChooser from which the user can
select a database connection. Invocation should be similar to
JFileChooser.showOpenDialog(). Thus my class contains the following
method which works fine:

public SheetConnection showConnectDialog() {
this.setModal(true);
this.setVisible(true);
this.dispose();
return this.sheetConnection;
}

This method displays the dialog and waits until the user selects a
connection to return an encapsulating object to the caller.

Now this looks somehow dirty to me. I dispose the current object and
afterwards I access one of its member variables to return it to the
caller. Is this technique OK? Is dispose posted to an event queue and
thus executed later (after leaving the method)? Obviously dispose is not
executed directly because in this case I should have received some run
time error?

I don't understand why you think that this is dirty. Perhaps the clue is
in your statement "I dispose the current object". dispose() does NOT
dispose of the current object.

JDialog inherits its dispose() method from java.awt.Window, whose javadocs
say this about dispose():

Releases all of the native screen resources used by this Window, its
subcomponents, and all of its owned children. That is, the resources for
these Components will be destroyed, any memory they consume will be
returned to the OS, and they will be marked as undisplayable.

Notice that dispose() returns resources to the operating system. It says
nothing about getting rid of the current object.

This is reinforced by the next paragraph in the javadocs:

The Window and its subcomponents can be made displayable again by
rebuilding the native resources with a subsequent call to pack or show.
The states of the recreated Window and its subcomponents will be
identical to the states of these objects at the point where the Window
was disposed (not accounting for additional modifications between those
actions).

Notice that everything can be brought back again IN ITS PRESENT STATE. The
"current object" is where the state information is kept, it has not been
eliminated. All that has to happen is that the native resources that were
released by disposse() have to be rebuilt.

There is nothing dirty about using dispose(). No queuing of the request is
necessary (as long as dispose() is executed on the EDT). Your code is not
going to be ripped out from under you as it is executing. All is well.

The javadocs are your friend.

Best wishes!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top