dialog boxes. joptionpane class allows you to display a dialog box small graphical window that...
TRANSCRIPT
![Page 1: Dialog Boxes. JOptionPane Class Allows you to display a dialog box Small graphical window that displays a message to the user or requests input Types:](https://reader036.vdocuments.net/reader036/viewer/2022071806/56649f575503460f94c7c0ac/html5/thumbnails/1.jpg)
Dialog Boxes
![Page 2: Dialog Boxes. JOptionPane Class Allows you to display a dialog box Small graphical window that displays a message to the user or requests input Types:](https://reader036.vdocuments.net/reader036/viewer/2022071806/56649f575503460f94c7c0ac/html5/thumbnails/2.jpg)
JOptionPane Class
Allows you to display a dialog box Small graphical window that displays a message to
the user or requests input
Types: Message Dialog Input Dialog
Need to import javax.swing.JOptionPane
![Page 3: Dialog Boxes. JOptionPane Class Allows you to display a dialog box Small graphical window that displays a message to the user or requests input Types:](https://reader036.vdocuments.net/reader036/viewer/2022071806/56649f575503460f94c7c0ac/html5/thumbnails/3.jpg)
Displaying Message Dialogs
showMessaegDialog method – used to display a message dialog
JOptionPane.showMessageDialog(null, “Hello World”);
First argument will be discussed when we learn about Graphical User Interfaces (GUIs) – used to display other graphical windows
Second argument is the message that we wish to display
![Page 4: Dialog Boxes. JOptionPane Class Allows you to display a dialog box Small graphical window that displays a message to the user or requests input Types:](https://reader036.vdocuments.net/reader036/viewer/2022071806/56649f575503460f94c7c0ac/html5/thumbnails/4.jpg)
Displaying Input Dialogs
Quick and easy way to enter data
String name; name = JOptionPane.showInputDialog(“Enter
your name.”);
![Page 5: Dialog Boxes. JOptionPane Class Allows you to display a dialog box Small graphical window that displays a message to the user or requests input Types:](https://reader036.vdocuments.net/reader036/viewer/2022071806/56649f575503460f94c7c0ac/html5/thumbnails/5.jpg)
Ending Program
When using the JOptionPane class, you must end a program Does not automatically stop executing when the end
of the main method is reached because the class causes an additional task(thread) to run
Need: System.exit(0); Needs an integer argument - value 0
traditionally indicates that the program ended successfully
![Page 6: Dialog Boxes. JOptionPane Class Allows you to display a dialog box Small graphical window that displays a message to the user or requests input Types:](https://reader036.vdocuments.net/reader036/viewer/2022071806/56649f575503460f94c7c0ac/html5/thumbnails/6.jpg)
Converting String Input to Numbers
Unlike the Scanner class, the JOptionPane class does not have different methods for reading values of different data types as input
showMessaegDialog method always returns the user’s input as a String, even if a numeric value was entered
What is the problem with this?
![Page 7: Dialog Boxes. JOptionPane Class Allows you to display a dialog box Small graphical window that displays a message to the user or requests input Types:](https://reader036.vdocuments.net/reader036/viewer/2022071806/56649f575503460f94c7c0ac/html5/thumbnails/7.jpg)
Converting String Input to Numbers
Use methods to convert string value to a numeric value
Method Example Code
Double.parseDouble
double num;num = Double.parseDouble(str)
Integer.parseInt int num;num = Integer.parseInt(str)
![Page 8: Dialog Boxes. JOptionPane Class Allows you to display a dialog box Small graphical window that displays a message to the user or requests input Types:](https://reader036.vdocuments.net/reader036/viewer/2022071806/56649f575503460f94c7c0ac/html5/thumbnails/8.jpg)
Example
int number; String str;
str = JOptionPane.showInputDialog(“Enter a number.”)
number = Integer.parseInt(str);
*Now you could use number to perform math methods*