*
The moose likes Beginning Java and the fly likes How does JOptionPane return the proper value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does JOptionPane return the proper value?" Watch "How does JOptionPane return the proper value?" New topic
Author

How does JOptionPane return the proper value?

Bob Barker
Greenhorn

Joined: May 14, 2011
Posts: 3
When using JOptionPane, the window will pop up, the user will be asked to select an option ( or give an input, etc ), and the method will return the value the user inputted.

How exactly does the program "pause" itself, to wait for the user to give an input, and how do I write code to do it as well (without extending the JOptionPane class)?
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
I think you're asking about modality. If a dialog is modal, that means it takes focus and doesn't allow anything else to happen within that program until its function is complete. A dialog that is not modal allows other program components to have focus as the user decides.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Bob Barker
Greenhorn

Joined: May 14, 2011
Posts: 3
Not so much the focus of the program. But, the program obviously has to wait until the user inputs something, selects the "close" button of the dialog box, or the cancel, or OK button. How does the program handle the "waiting for the user"?
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Okay, then the result of the user's selection is returned when the OK button is pressed. You might better understand the functionality from the API. It just works the way it works.
Bob Barker
Greenhorn

Joined: May 14, 2011
Posts: 3
Example. When extending JOptionPane, you can call getValue() on the JOptionPane instance. This will return the value that the user selected. As far as I can tell, this method will cause the program to pause until the user has given some input. It will never return a value that indicates, "the user has not yet inputted a value". Clearly, the program has to be doing something that causes it to wait. What is that?
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
GUI programs such as those written with Swing are generally "event-driven", meaning that the code the programmer writes gets called by some framework based on events that happen outside the program. In the case of a GUI program, the events are things that the end-user does. In Swing, these are generally "listeners" of whatever kind: action listeners, keypress listeners, etc., etc.

So with that background in mind: you set up a JOptionDialog and call its show method, and it sets up listeners and pauses the thread it was called on so it can respond to the user events. Then it resumes the thread you called it on and returns to you.

rc
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How does JOptionPane return the proper value?
 
Similar Threads
Message Dialog box --> action performed.
JOptionPane Timeout
intercepting return from JOptionPane
How can i get specific button in JOptionaPne
JOptionPane and Arrays