CSC161 - Dialog Boxes

The JOptionPane class can be used to create a dialog box, which is a small pop up window. The dialog box contains a message and optionally, you can use it to print a message and read a value in response.

To use JOptionPane, the following import statement must be included in your program:

     import javax.swing.JOptionPane;

JOptionPane contains two methods which display dialog boxes on the screen. The showMessageDialog method displays a dialog box with a message. The showInputDialog method displays a dialog box with a message and a text field. It returns a String which contains the data that the user entered into the text field.

Message Dialogs

To display a message dialog, use the showMessageDialog method:

     JOptionPane.showMessageDialog(null,"Hi Mom!");

This displays the following dialog box:

The first parameter of showMessageDialog is used in programs that have other graphical elements on the screen. Since our programs are only using dialog boxes, we will pass null as the first parameter to this method.

Input Dialogs

To display a input dialog, use the showInputDialog method:

     String team;
     team = JOptionPane.showInputDialog(null,"Who is your favorite team?");

This displays the following dialog box:

The input dialog box contains a text field in which the user can enter text. When the user clicks on OK, the data entered by the user is returned by the showInputDialog and copied into the String variable on the lhs of the assignment statement. Notice that there is only one parameter to this method.

System.exit

A program that uses JOptionPane creates an additional task in the JVM, to manage the graphics. When the program ends, this additional task is not terminated automatically. To make sure that this task is terminated, you must include the System.exit(0) statement at the end of your program. This statement causes the program to end, and ends all tasks that are part of the program. The parameter value of 0 indicates that the program ended without any errors.

Converting Value Retuned by showInputDialog

The JOptionPane does not have different methods for reading values of different types. Every call to showInputDialog returns a String. If you enter an int such as the number 144, it will be returned at the String "144". To convert the value into another type, use one of the following methods:

Method Purpose Example
Byte.parseByte convert a String to a byte
   byte bnum;
   bnum = Byte.parseByte(str);
Double.parseDouble convert a String to a double
   double dnum;
   dnum = Double.parseDouble(str);
Float.parseFloat convert a String to a float
   float fnum;
   fnum = Float.parseFloat(str);
Integer.parseInt convert a String to an int
   int inum;
   inum = Integer.parseInt(str);
Long.parseLong convert a String to a long
   long lnum;
   lnum = Long.parseLong(str);
Short.parseShort convert a String to a short
   short snum;
   snum = Short.parseShort(str);

Examples

     int age;
     String str;
     str = JOptionPane.showInputDialog("Enter your age:");
     age = Integer.parseInt(str);
     double price;
     String str;
     str = JOptionPane.showInputDialog("Enter the retail price:");
     price = Double.parseDouble(str);


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2013