Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2006

Tip: Looking for answers? Try searching our database.

How to pass back a value from a window

Thread view: 
PJava - 26 Oct 2006 04:41 GMT
If you use JOptionPane.showInputDialog, it will pass back a string
value.

I have a JDialog window, but don't know how to pass back an object
after the window closed.

Please kindly give me some idea!

THANKS!!!
Lionel - 26 Oct 2006 06:22 GMT
> If you use JOptionPane.showInputDialog, it will pass back a string
> value.
[quoted text clipped - 5 lines]
>
> THANKS!!!

Extend the JDialog class. Create your gui by setting the JDialog
contentPane or adding to it. Add a static method such as:

public static String getString(JFrame parent) {}

This method will be blocked when you call setVisible(true). Have a
global string value that is set by interaction with the user or a field
that has a string entered into it and return this string:

public class StringDialog extends JDialog {
    String globalString;

    public StringDialog(JFrame parent) {
        super(parent, true);
        //write gui code here
    }

    public static String getString(JFrame parent) {
        StringDialog strDialog = new StringDialog(parent);

        strDialog.pack();
        strDialog.setVisible(true);

        return globalString;
    }
}
Oliver Wong - 26 Oct 2006 15:02 GMT
>> If you use JOptionPane.showInputDialog, it will pass back a string
>> value.
[quoted text clipped - 32 lines]
>     }
> }

   First of all, you're accessing an instance field (globalString) from a
static method (getString).

   But even if you fix that, I think when getString() is called, the JFrame
will be made visible, and the method will immediately return, regardless of
what the user does. Most of the time, it'll return so fast, the user won't
have time to do anything ,and so the getString() method will always return
the empty string.

   You need to . stop processing until the user dismisses the JFrame, in
which case it's probably a better idea to use a JDialog than a JFrame
anyway.

   - Oliver
Lionel - 26 Oct 2006 22:09 GMT
>>> If you use JOptionPane.showInputDialog, it will pass back a string
>>> value.
[quoted text clipped - 32 lines]
>>     }
>> }

I whipped up the code without thinking but ...

>     First of all, you're accessing an instance field (globalString) from a
> static method (getString).

Yeap, should be static.

>     But even if you fix that, I think when getString() is called, the JFrame
> will be made visible, and the method will immediately return, regardless of
> what the user does. Most of the time, it'll return so fast, the user won't
> have time to do anything ,and so the getString() method will always return
> the empty string.

Nope, it blocks the static method until setVisible(false); or the x is
pressed.

>     You need to . stop processing until the user dismisses the JFrame, in
> which case it's probably a better idea to use a JDialog than a JFrame
> anyway.

Huh?

Re-read:

public class StringDialog extends JDialog {...}
Oliver Wong - 27 Oct 2006 15:01 GMT
> Huh?
>
> Re-read:
>
> public class StringDialog extends JDialog {...}

   This is the part I misread. For some reason, I thought you were
extending JFrame.

   - Oliver
Lionel - 01 Nov 2006 00:09 GMT
>> Huh?
>>
[quoted text clipped - 4 lines]
>     This is the part I misread. For some reason, I thought you were
> extending JFrame.

Good thing I forgot to post my code then :). And of course, if I was
extending JFrame then you would have been spot on. The confusion
probably came about because I said that you design the JDialog
contentpane in a similar way to a JFrame contentpane.

Lionel.
Lionel - 26 Oct 2006 23:31 GMT
[...]

I'll post a full code later if you don't understand. You might find it
useful. I certainly find it a nice way to make dialog's just the way you
want them.

Lionel.
Karsten Lentzsch - 26 Oct 2006 11:11 GMT
> If you use JOptionPane.showInputDialog, it will pass back a string
> value.
>
> I have a JDialog window, but don't know how to pass back an object
> after the window closed.

Give your dialog a model, often a single object,
more generally an object graph. Bind the dialog's
UI compontent values to that object.

Read model values after the dialog has been closed
(modal), or access the model while the dialog is open
(non-modal).

-Karsten
Jeff - 26 Oct 2006 19:52 GMT
> If you use JOptionPane.showInputDialog, it will pass back a string
> value.
[quoted text clipped - 5 lines]
>
> THANKS!!!

Assuming you have an OK button on your dialog that you are using to
close, in the event listener for the button use
myDialog.setVisible(false);

Then you can read the fields from the dialog, even though it is no
longer displayed, at any time. No need to pass the object, just read
it.
/js
PJava - 26 Oct 2006 22:46 GMT
Thank all,

Give me a lot of response. I newed a JDialog with a true modal value.
When click OK button, the dialog is setVisible(false).

I get the value by getResult() which defined in the window class
called. After getting the result, I dispose the dialog. It works well.

I appreciate your helpful suggestion and help!

> > If you use JOptionPane.showInputDialog, it will pass back a string
> > value.
[quoted text clipped - 14 lines]
> it.
> /js
steve - 28 Oct 2006 23:11 GMT
> Thank all,
>
[quoted text clipped - 24 lines]
>> it.
>> /js

Oh yes , you also missed your try & catch block, in which case you may not
return via the return.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.