JOptionPane class
Dialog Boxes
• A dialog box is a small graphical window that
displays a message to the user or requests
input.
• A variety of dialog boxes can be displayed
using the JOptionPane class.
• Two of the dialog boxes are:
– Message Dialog - a dialog box that displays a
message.
– Input Dialog - a dialog box that prompts the user
for input.
Using the import Statement
• The JOptionPane class is not
automatically available to your Java
programs.
• The following statement must be before
the program’s class header:
import [Link];
• This statement tells the compiler where
to find the JOptionPane class.
Dialog Boxes
The JOptionPane class provides static methods to
display each type of dialog box.
Message Dialogs
• [Link] method is
used to display a message dialog.
[Link](null,
"Hello World");
• The second argument is the message that is
to be displayed
Input Dialogs
• An input dialog is a quick and simple
way to ask the user to enter data.
• The dialog displays a text field, an Ok
button and a Cancel button.
• If Ok is pressed, the dialog returns the
user’s input.
• If Cancel is pressed, the dialog returns
null.
Input Dialogs
String name;
name = [Link](
"Enter your name.");
• The argument passed to the method is the message
to display.
• If the user clicks on the OK button, name
references the string entered by the user.
• If the user clicks on the Cancel button, name
references null.
[Link]
import [Link];
public class NamesDialog
{
public static void main(String[] args)
{
String firstName; // The user's first name
String middleName; // The user's middle name
String lastName; // The user's last name
// Get the user's first name
firstName =
[Link]("What is " +
"your first name? ");
[Link]
// Get the user's middle name.
middleName =
[Link](
"What is " + "your middle name? ");
// Get the user's last name.
lastName =
[Link]("What
is " + "your last name? ");
Example
// Display a greeting
[Link](null,
"Hello " + firstName + " " +middleName
+ " " + lastName);
[Link](0);
}
}
The [Link]() Method
• A program that uses JOptionPane does
not automatically stop executing when
the end of the main method is reached.
• Java generates a thread, which is a
process running in the computer, when a
JOptionPane is created.
• If the [Link] method is not called,
this thread continues to execute.
The [Link]() Method
• The [Link] method requires an integer
argument.
[Link](0);
• This argument is an exit code that is passed
back to the operating system.
• This code is usually ignored, however, it can
be used outside the program:
– to indicate whether the program ended successfully
or as the result of a failure.
– The value 0 traditionally indicates that the program
ended successfully.
Converting a String to a
Number
• The JOptionPane’s showInputDialog
method always returns the user's input
as a String
• String containing a number, such as
“127.89, can be converted to a numeric
data type.
The Parse Methods
• Parse methods convert strings to
numeric data types
• They are:
– [Link]
– [Link]
– [Link]
– [Link]
– [Link]
– [Link]
The Parse Methods- Examples
• byte bVar = [Link]("1");
• int iVar = [Link]("2599");
• short sVar = [Link]("10");
• long lVar = [Link]("15908");
• float fVar = [Link]("12.3");
• double dVar =
[Link]("7945.6");
[Link]
import [Link];
public class PayrollDialog
{
public static void main(String[] args)
{
String inputString; // For reading input
String name; // The user's name
int hours; // The number of hours worked
double payRate; // The user's hourly pay rate
double grossPay; // The user's gross pay
[Link]
// Get the user's name.
name = [Link]("What
is " + "your name? ");
// Get the hours worked.
inputString =
[Link](
"How many hours” +
“ did you work this week? ");
// Convert the input to an int.
hours = [Link](inputString);
[Link]
// Get the hourly pay rate.
inputString =
[Link]("What is” +
" your hourly pay rate? ");
// Convert the input to a double.
payRate =
[Link](inputString);
// Calculate the gross pay.
grossPay = hours * payRate;
[Link]
// Display the results.
[Link](null,
"Hello " + name + ". Your gross pay is $" +
grossPay);
// End the program.
[Link](0);
}
}