Obtaining input from user via swing component through static method.

J

John

Hi.

This sounds easy to do but I've been struggling with getting a clean and
reliable solution.

I would like to make a call like this...

Object value = UserQueryAgent.getName();

.... in order to obtain the user's name.

UserQueryAgent.getName() is a static method which creates a
JInternalFrame (say), waits until a condition is met (typically the user
providing the required information), then returns the value provided to
the JInternalFrame.

How would you code UserQueryAgent.getName()?

I can get it to work, but only in a fairly scrappy fashion which is
prone to failing with bizarre exceptions.

John
 
G

Gerrit Hulleman

Have you tried a modal dialog? Create a dialog using a constructor that
offers modal (see java api). When the show methode of this dialog is called,
the main 'thread' will wait for the dialog to 'hide'. When the dialog hides,
the dialog object can be queried for the information. Retrieve the
information from the dialog and return as result.

Gerrit
 
T

Thomas Weidenfeller

John said:
UserQueryAgent.getName() is a static method which creates a
JInternalFrame (say), waits until a condition is met (typically the user
providing the required information), then returns the value provided to
the JInternalFrame.

One usually does not wait for user input in a GUI system (read up on
event-driven programming). It does not work in general. There is one
common exception where it can be made to work, when a modal window is
used. In your case, usage of a modal window might be possible. Have I
look at JOptionPane.

May I suggest that you also spend some time to learn about GUI
programming in general, and Java GUI programming in particular? The
comp.lang.java.gui FAQ points to a few resources, like tutorials and
architecture papers.

/Thomas
 
J

John

Gerrit said:
Have you tried a modal dialog? Create a dialog using a constructor that
offers modal (see java api). When the show methode of this dialog is called,
the main 'thread' will wait for the dialog to 'hide'. When the dialog hides,
the dialog object can be queried for the information. Retrieve the
information from the dialog and return as result.

Gerrit

This will be modal (I can handle that bit), but I would like to be able
to use any component in the same way as the JOptionPane you mentioned (I
don't think this will work for JInternalFrames for example.)
 
J

John

John said:
This will be modal (I can handle that bit), but I would like to be able
to use any component in the same way as the JOptionPane you mentioned (I
don't think this will work for JInternalFrames for example.)

To clarify: I know you can use JOptionPane for basic questions, but I
need more versatility.

John
 
J

John

Thomas said:
One usually does not wait for user input in a GUI system (read up on
event-driven programming). It does not work in general. There is one
common exception where it can be made to work, when a modal window is
used. In your case, usage of a modal window might be possible. Have I
look at JOptionPane.

This is such an exceptional case. I know about JOptionPane, but it is
too limited for my use.
May I suggest that you also spend some time to learn about GUI
programming in general, and Java GUI programming in particular?

No you may not. It is patronising and unnecessary.
The
comp.lang.java.gui FAQ points to a few resources, like tutorials and
architecture papers.

The fact that I have asked a question about GUIs doesn't mean that I
have no experience with them. Thankyou for your response, but please
give me credit for being able to define my own requirements and use a
search engine or newsreader.

John
 
G

Gerrit Hulleman

John said:
To clarify: I know you can use JOptionPane for basic questions, but I
need more versatility.

John

In that case I think the only way is to start a seperate thread with what
ever source you need(be it panes, frame or automobiles) , and wait the
parent thread for the client thread to end.

Gerrit
 
T

Tor Iver Wilhelmsen

John said:
To clarify: I know you can use JOptionPane for basic questions, but I
need more versatility.

JOptionPane is more than just the static methods; read the docs for a
lot of nice hints about what you can use it for. :)
 
N

Nigel Wade

To clarify: I know you can use JOptionPane for basic questions, but I
need more versatility.

John

JOptionPane is quite versatile, you can put various components into one.
For example, I have a dialog which requests a userid and password in a
JOptionPane. The text on the dialog is customised, the userid is entered
in a JTextField and the password in a JPasswordField.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top