JDialogs
I have a program that requires two inputs from the user, and they both have to be validated. They are input when a user clicks a button. I want to know how to display a JDialog or JFrame that can notify, or otherwise set the code in the main thread back into action after input is accepted. I've tried notify/wait but that doesn't work because I'm not synchronising anything.
I want the main thread/frame to stop executing until the user input is acceptable validated and then continue. How can I do this?
[521 byte] By [
Vagabona] at [2007-11-27 6:58:15]

> I have a program that requires two inputs from the
> user, and they both have to be validated. They are
> input when a user clicks a button. I want to know how
> to display a JDialog or JFrame that can notify, or
> otherwise set the code in the main thread back into
> action after input is accepted. I've tried
> notify/wait but that doesn't work because I'm not
> synchronising anything.
>
> I want the main thread/frame to stop executing until
> the user input is acceptable validated and then
> continue. How can I do this?
If I am reading you correctly, you want a JDialog to act in a modal fashion: the application halts until the JDialog is dealt with. The key is in how you call the JDialog's constructor. Read up on JDialog in the java documentation. Also look at the examples. They'll probably show you what you want.
Also, if all you need is a yes / no or yes/ no / cancel input, or a simple text input, then look into JOptionPane which is a simple dialog that is very easy to set up.
Otherwise, if you need more specific help, you'll have to provide more information including some of your code.
I looked at the JavaDocs and then the link on it one how to make JDialogs but that was all about JOptionPane.
This is exactly what the program needs to do:
When user clicks connect button, launch a modal dialog box that asks for both Host and Port number. Host needs to be verified that it is a URL and Port needs to be verified that ti is a number. The dialog will then try to return an active Socket to host program. Host continues execution.
> I looked at the JavaDocs and then the link on it one
> how to make JDialogs but that was all about
> JOptionPane.
If you look at the next few pages of the examples, you'll see general information on JDialog.
> This is exactly what the program needs to do:
> When user clicks connect button, launch a modal
> dialog box that asks for both Host and Port number.
> Host needs to be verified that it is a URL and Port
> needs to be verified that ti is a number. The dialog
> will then try to return an active Socket to host
> program. Host continues execution.
In the javadocs page under JDialog in the constructor section it states:
public JDialog(Dialog owner,
String title,
boolean modal)
throws HeadlessExceptionCreates a modal or non-modal dialog with the specified title and the specified owner frame.
This is what you want.
go to the Swing examples page:[url= http://java.sun.com/docs/books/tutorial/uiswing/examples/components/index.html#DialogDemo][/url]and download both DialogDemo.java and CustomDialog.java.Those two sample files will have what you want, I believe.
> > I looked at the JavaDocs and then the link on it
> one
> > how to make JDialogs but that was all about
> > JOptionPane.
>
> If you look at the next few pages of the examples,
> you'll see general information on JDialog.
>
> > This is exactly what the program needs to do:
> > When user clicks connect button, launch a modal
> > dialog box that asks for both Host and Port
> number.
> > Host needs to be verified that it is a URL and
> Port
> > needs to be verified that ti is a number. The
> dialog
> > will then try to return an active Socket to host
> > program. Host continues execution.
>
> In the javadocs page under JDialog in the constructor
> section it states:
> [code]
> public JDialog(Dialog owner,
>String title,
> boolean modal)
> throws HeadlessExceptionCreates a modal or
> non-modal dialog with the specified title and the
> specified owner frame.
> /code]
> This is what you want.
But how do I get it to display, then return? I might have it though...I don't know, I'm taking a break for the moment
> But how do I get it to display, then return? I might
> have it though...I don't know, I'm taking a break for
> the moment
I'm sorry but I'm not sure that I fully understand the question. Maybe someone else can answer better. I think that to end a custom dialog's interaction with the user, you setVisible(false) for the dialog.