returned data from dispose()'d JDialog

Discussion in 'Java' started by Digital Puer, May 10, 2004.

  1. Digital Puer

    Digital Puer Guest

    I have a custom JDialog that is supposed to return several pieces
    of data from the user. From the Java tutorial, I learned that one
    good way is to have the JDialog provide accessor methods to
    return the data.

    Question:
    Suppose I have already called the accessor methods to get back, say,
    an int, an Integer, and a Vector. Then I call JDialog's dispose().
    I am not sure about the return-by-value semantics of the above
    types, so I do not know if the data will still be valid. Would I
    have to do some kind of cloning??

    e.g.

    MyJDialog dia = new MyJDialog(...);

    int int1 = dia.get_int();
    Integer int2 = dia.get_Integer();
    Vector vec = dia.get_vector();

    dia.dispose();

    // ---> will int1, int2, and vec still be valid here?
     
    Digital Puer, May 10, 2004
    #1
    1. Advertising

  2. Digital Puer

    Woebegone Guest

    "Digital Puer" <> wrote in message
    news:...
    > I have a custom JDialog that is supposed to return several pieces
    > of data from the user. From the Java tutorial, I learned that one
    > good way is to have the JDialog provide accessor methods to
    > return the data.
    >
    > Question:
    > Suppose I have already called the accessor methods to get back, say,
    > an int, an Integer, and a Vector. Then I call JDialog's dispose().
    > I am not sure about the return-by-value semantics of the above
    > types, so I do not know if the data will still be valid. Would I
    > have to do some kind of cloning??
    >
    > e.g.
    >
    > MyJDialog dia = new MyJDialog(...);
    >
    > int int1 = dia.get_int();
    > Integer int2 = dia.get_Integer();
    > Vector vec = dia.get_vector();
    >
    > dia.dispose();
    >
    > // ---> will int1, int2, and vec still be valid here?


    Yes; the primitive int1 is copied by value, so no problem there. int2 and
    vec are object references, and since they are maintained the objects they
    reference won't be garbage collected (or /delete/d :)). Not like, say, C++
    where returning a reference to a local can be lethal.

    Sean.
     
    Woebegone, May 11, 2004
    #2
    1. Advertising

  3. Digital Puer wrote:
    > I have a custom JDialog that is supposed to return several pieces
    > of data from the user. From the Java tutorial,


    Could you provide a reference to that particular tutorial and statement,
    please, because ...

    > I learned that one
    > good way is to have the JDialog provide accessor methods to
    > return the data.


    .... I don't think it is a really good way in the general case. In
    general one would not expect to have to "pull" data out of a JDialog,
    but instead would expect that the JDialog "pushes" changes to its model.
    This would be in line with the general ideas of MVC, and the variant as
    implemented in Swing.

    There is a common exception in Swing: the kind of alert and message
    boxes as implemented in JOptionPane, or in JFileChooser. Here the
    dialogs are modal, and processing is supposed to continue only when the
    modal dialog is done. This provides clear timing when the data is
    supposed to be pulled out of the remains of the dialog.

    > MyJDialog dia = new MyJDialog(...);
    >
    > int int1 = dia.get_int();
    > Integer int2 = dia.get_Integer();
    > Vector vec = dia.get_vector();


    How do you know that is was time to call your get_...() methods? How do
    you know that the user had finished the editing? Could it be, that in
    reality you have the call to the get_...() methods in an event handler?
    Because, this would then really be the event handler supposed to push
    the data into the model.

    BTW: Please consider adapting the usual Java naming convention for
    accessors methods: getUppercaseLetter, not get_lowercase_letter. Same
    for manipulators: setUppercaseLetter, not set_lowercase_letter.


    > // ---> will int1, int2, and vec still be valid here?


    Yes, but unless you have some extra code (which you didn't show) which
    ensured that you got the data at the right time, they will contain some
    intermediate editing state and maybe not what you expect.

    /Thomas
     
    Thomas Weidenfeller, May 11, 2004
    #3
  4. Digital Puer

    Digital Puer Guest

    Thomas Weidenfeller wrote:
    > Digital Puer wrote:
    >
    >> I have a custom JDialog that is supposed to return several pieces
    >> of data from the user. From the Java tutorial,

    >
    >
    > Could you provide a reference to that particular tutorial and statement,
    > please, because ...



    I am writing a custom modal JDialog. The Tutorial says:

    http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html#input

    "If you're designing a custom dialog, you need to design your dialog's API so
    that you can query the dialog about what the user chose. For example,
    CustomDialog has a getValidatedText method that returns the text the user
    entered."




    >
    >> MyJDialog dia = new MyJDialog(...);
    >>
    >> int int1 = dia.get_int();
    >> Integer int2 = dia.get_Integer();
    >> Vector vec = dia.get_vector();

    >
    >
    > How do you know that is was time to call your get_...() methods? How do
    > you know that the user had finished the editing? Could it be, that in
    > reality you have the call to the get_...() methods in an event handler?
    > Because, this would then really be the event handler supposed to push
    > the data into the model.


    I am writing a modal JDialog, so after the line with the constructor,
    I know the user has either pressed "OK" or "cancel".



    >
    > BTW: Please consider adapting the usual Java naming convention for
    > accessors methods: getUppercaseLetter, not get_lowercase_letter. Same
    > for manipulators: setUppercaseLetter, not set_lowercase_letter.
    >
    >


    I didn't like that coding convention when I programmed in Pascal 14 years
    ago, and I definitely don't like it now.
     
    Digital Puer, May 11, 2004
    #4
  5. Digital Puer

    Digital Puer Guest

    Woebegone wrote:

    > "Digital Puer" <> wrote in message
    > news:...
    >
    >>I have a custom JDialog that is supposed to return several pieces
    >>of data from the user. From the Java tutorial, I learned that one
    >>good way is to have the JDialog provide accessor methods to
    >>return the data.
    >>
    >>Question:
    >>Suppose I have already called the accessor methods to get back, say,
    >>an int, an Integer, and a Vector. Then I call JDialog's dispose().
    >>I am not sure about the return-by-value semantics of the above
    >>types, so I do not know if the data will still be valid. Would I
    >>have to do some kind of cloning??
    >>
    >>e.g.
    >>
    >>MyJDialog dia = new MyJDialog(...);
    >>
    >>int int1 = dia.get_int();
    >>Integer int2 = dia.get_Integer();
    >>Vector vec = dia.get_vector();
    >>
    >>dia.dispose();
    >>
    >>// ---> will int1, int2, and vec still be valid here?

    >
    >
    > Yes; the primitive int1 is copied by value, so no problem there. int2 and
    > vec are object references, and since they are maintained the objects they
    > reference won't be garbage collected (or /delete/d :)). Not like, say, C++
    > where returning a reference to a local can be lethal.
    >




    If, as you say, the Integer and Vector objects in MyJDialog will not
    be garbage collected, will the rest of the member data in the MyJDialog
    be GC'ed when dispose() is called? If the answer is no, should I clone()
    the data to release the memory reference to the Integer and Vector?
     
    Digital Puer, May 11, 2004
    #5
  6. Digital Puer wrote:

    > Woebegone wrote:
    >
    >> "Digital Puer" <> wrote in message
    >> news:...
    >>
    >>>I have a custom JDialog that is supposed to return several pieces
    >>>of data from the user. From the Java tutorial, I learned that one
    >>>good way is to have the JDialog provide accessor methods to
    >>>return the data.
    >>>
    >>>Question:
    >>>Suppose I have already called the accessor methods to get back, say,
    >>>an int, an Integer, and a Vector. Then I call JDialog's dispose().
    >>>I am not sure about the return-by-value semantics of the above
    >>>types, so I do not know if the data will still be valid. Would I
    >>>have to do some kind of cloning??
    >>>
    >>>e.g.
    >>>
    >>>MyJDialog dia = new MyJDialog(...);
    >>>
    >>>int int1 = dia.get_int();
    >>>Integer int2 = dia.get_Integer();
    >>>Vector vec = dia.get_vector();
    >>>
    >>>dia.dispose();
    >>>
    >>>// ---> will int1, int2, and vec still be valid here?

    >>
    >>
    >> Yes; the primitive int1 is copied by value, so no problem there. int2 and
    >> vec are object references, and since they are maintained the objects they
    >> reference won't be garbage collected (or /delete/d :)). Not like, say,
    >> C++ where returning a reference to a local can be lethal.
    >>

    >
    >
    >
    > If, as you say, the Integer and Vector objects in MyJDialog will not
    > be garbage collected, will the rest of the member data in the MyJDialog
    > be GC'ed when dispose() is called? If the answer is no, should I clone()
    > the data to release the memory reference to the Integer and Vector?


    The members of the dialog will be eligible for GC once the dialog itself is
    eligible for GC.

    Just calling dispose() on a dialog does not make it eligible for GC, since
    you can still have other references to it. Disposing a dialog just releases
    the (native) resources used by it.

    Read the javadocs of dispose again:
    <http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Window.html#dispose()>

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, May 11, 2004
    #6
  7. Digital Puer wrote:
    > Thomas Weidenfeller wrote:
    >> Could you provide a reference to that particular tutorial and
    >> statement, please, because ...

    >
    > I am writing a custom modal JDialog. The Tutorial says:
    >
    > http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html#input
    >
    > "If you're designing a custom dialog, you need to design your dialog's
    > API so that you can query the dialog about what the user chose. For
    > example, CustomDialog has a getValidatedText method that returns the
    > text the user entered."
    >


    That's in the context of JOptionPane. It is a bad idea for a
    general-purpose dialog window.

    > I am writing a modal JDialog, so after the line with the constructor,
    > I know the user has either pressed "OK" or "cancel".


    The constructor does all the work (show, hide, etc.)? That is a really
    bad idea. But do as you please.

    > I didn't like that coding convention when I programmed in Pascal 14 years
    > ago, and I definitely don't like it now.


    Well, you got your advice. Feel free to ignore it. I won't bother you again.

    /Thomas
     
    Thomas Weidenfeller, May 13, 2004
    #7
    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. SS
    Replies:
    2
    Views:
    479
    Trond-Eirik Kolloen
    Jul 3, 2003
  2. Antonio Concepcion
    Replies:
    3
    Views:
    2,844
    Antonio Concepcion
    Feb 17, 2005
  3. terry
    Replies:
    1
    Views:
    590
    VisionSet
    Jun 4, 2004
  4. Michael Carr
    Replies:
    2
    Views:
    377
    Edwin Knoppert
    May 16, 2006
  5. Replies:
    2
    Views:
    618
    Ian Wilson
    Jan 16, 2007
Loading...

Share This Page