Java Lesson 3: JOptionPane Dialog Boxes and Data Parsing
Key Points:
The JOptionPane is a class in Java that allows you to display a dialog box quickly. By doing this you can simply display info or request info from the user.
Inorder to use this you must import the class at the beginning of the program: import javax.swing.JOptionPane;
To do an input dialog box: String name = JOptionPane.showInputDialog(“Enter text” + variableName);
To do an output dialog box: JOptionPane.showMessageDialog(null, “Text” + variableName2);
When you use the showInputDialog the informaiton gets returned as a string, so if you want the information as a different variable type you must convert it (parse it). See table below for each data type:
String to short
Short.parseShort
String to long
Long.parseLong
String to integer
Integer.parseInt
String to float
Float.parseFloat
String to double
Double.parseDouble
String to byte
Byte.parseByte
Example of data parse in JOptionPane: String inputAge = JOptionPane.showInputDialog(“How old are you?”); int age = Integer.parseInt(inputAge);