Showing posts with label showInputDialog. Show all posts
Showing posts with label showInputDialog. Show all posts

Monday, April 23, 2012

JOptionPane

From Swing, a JOptionPane is a dialog window.

http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

The showInputDialog function returns a String:

String thisString = JOptionPane.showInputDialog("Please input a value");

If you need to use the return value as an int, you may use the following function:

int returnValue = Integer.parseInt(thisString);

(But if the string contains anything other than numbers !!! Your program will CRASH)

If the user presses the Cancel button, showInputDialog will return null.

The following snippet checks if the return is (not) equal to null:

if ((thisString != null) {
// Statements....
}