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

8
Dialog Boxes

Upload: marcus-brooks

Post on 13-Dec-2015

214 views

Category:

Documents


1 download

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:

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:

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:

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:

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:

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:

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:

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:

Example

int number; String str;

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

number = Integer.parseInt(str);

*Now you could use number to perform math methods*