returned data from dispose()'d JDialog

D

Digital Puer

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?
 
W

Woebegone

Digital Puer said:
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.
 
T

Thomas Weidenfeller

Digital said:
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
 
D

Digital Puer

Thomas said:
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."



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.
 
D

Digital Puer

Woebegone said:
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?
 
C

Christophe Vanfleteren

Digital said:
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()>
 
T

Thomas Weidenfeller

Digital said:
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
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top