Professional Documents
Culture Documents
JOption Pane
JOption Pane
Java Programming
Three basic syntax option for Input Dialog box: (This method returns a String value)
JOptionPane.showInputDialog(String 1)
JOptionPane.showInputDialog(null, String 1)
JOptionPane.showInputDialog(null, String 1, String 2, Dialog Type)
Example code:
JOptionPane.showInputDialog(“Enter your name:”)
JOptionPane.showInputDialog(null, “Enter your name:”)
JOptionPane.showInputDialog(null, “Enter your name”, “Entry Form”,
JOptionPane.INFORMATION_MESSAGE)
Result:
String2
String1
Dialog
type
Conversion of String: Whatever the user enters will always be return to your code as a
String value. Therefore, if you want to keep it as a string, just store it in a String variable.
However, to use the information as another data type, you must convert the String to the
appropriate data type.
Example code:
String userInput; //again identifier userInput solely for example purposes
userInput = JOptionPane.showInputDialog(“Enter”);
String2
String1
Dialog
type
Example code/output of message dialog box with just a String output and default title
bar and dialog type:
Example code/output of message dialog box with a String output, a title bar String and
dialog type: (Please note the three commas!)