dialog boxes. joptionpane class allows you to display a dialog box small graphical window that...

Post on 13-Dec-2015

214 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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: Message Dialog Input Dialog

Need to import javax.swing.JOptionPane

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

Displaying Input Dialogs

Quick and easy way to enter data

String name; name = JOptionPane.showInputDialog(“Enter

your name.”);

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

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?

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)

Example

int number; String str;

str = JOptionPane.showInputDialog(“Enter a number.”)

number = Integer.parseInt(str);

*Now you could use number to perform math methods*

top related