Dirty dispose?

Discussion in 'Java' started by Andreas Thiele, Mar 10, 2006.

  1. 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
    Andreas Thiele, Mar 10, 2006
    #1
    1. Advertising

  2. Andreas Thiele wrote:
    > 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
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
    Thomas Weidenfeller, Mar 10, 2006
    #2
    1. Advertising

  3. So my method is OK :)

    Andreas
    Andreas Thiele, Mar 10, 2006
    #3
  4. Andreas Thiele wrote:
    > 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?

    --
    John Bollinger
    John C. Bollinger, Mar 12, 2006
    #4
  5. Andreas Thiele

    Ian Shef Guest

    "Andreas Thiele" <> wrote in
    news:durm5s$2fi$03$-online.com:

    > 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!

    --
    Ian Shef 805/F6 * These are my personal opinions
    Raytheon Company * and not those of my employer.
    PO Box 11337 *
    Tucson, AZ 85734-1337 *
    Ian Shef, Mar 16, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    519
  2. =?Utf-8?B?U2hhd24=?=

    Set DataGridItem dirty

    =?Utf-8?B?U2hhd24=?=, Sep 7, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    333
    =?Utf-8?B?U2hhd24=?=
    Sep 7, 2004
  3. Antonio Concepcion
    Replies:
    3
    Views:
    2,691
    Antonio Concepcion
    Feb 17, 2005
  4. EDom

    Page is Dirty information

    EDom, Aug 9, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    387
    =?Utf-8?B?QW5kcmVhcy5LbnVkc2VuKEFUKWJla2subm8=?=
    Aug 23, 2005
  5. Replies:
    3
    Views:
    1,628
    Bruce Barker
    Mar 20, 2006
Loading...

Share This Page