COP 3337 - Programming II - Java Dialog I/O

Please review this material carefully, it will enable you to work on Assignment 5.

In addition to console input and output (to be discussed later in class), Java has a simple mechanism to ask a user questions and to receive answers.
The Java class JOptionPane is commonly used for this purpose. The class JOptionPane resides in package javax.swing.
It defines the following useful static methods

In the following I will explain and give examples of each method.

JOptionPane.showMessageDialog

This static method is meant to announce some text to the user. The user will be presented with a dialog box that contains a single button. The user acknowledges the message from the program by pressing the button. While the message is shown to the user, the program waits.

Example:

import javax.swing.*;

class Test1 {
	public static void main(String args[]) {
		JOptionPane.showMessageDialog(null, "hello world" );
		System.exit(0);
	}
}
when run, the program produces this message box:  

Explanation of parameters:

null
means that there is no parent frame, that is the dialog is the only frame.
"hello world"
is the string that is displayed in the dialog box.
There are 2 more showMessageDialog methods in the JOptionPane class. They have additional parameters to change the information symbol into others, and to change the title of the dialog box.

Note ! The Java line "System.exit(0);" is added to ensure that the program ends once the dialog is completed. If you want your program to continue after it returns from the dialog, then don't use this line. Use the line as the final line in your main program.

JOptionPane.showConfirmDialog

This static method is meant to get answers from the user. In its simplest form the user can select from a "yes", "no" or "cancel" button.

Example:

import javax.swing.*;

class Test2 {
	public static void main(String args[]) {
		int i = JOptionPane.showConfirmDialog(null, "hello world" );
		System.out.println("result: " + i);
		System.exit(0);
	}
}
when run, the program produces this message box:  

Explanation of parameters:

null
means that there is no parent frame, that is the dialog is the only frame.
"hello world"
is the string that is displayed in the dialog box.
Notice that the showConfirmDialog method has a return value of type int. It indicates which button the user has pressed. The "yes" button return 0, the "no" button returns 1, the "cancel" button returns 2.
There are 3 more showConfirmDialog methods in the JOptionPane class. They have additional parameters to change the question-mark symbol into others, to change the title of the dialog box, and to change the button choices that are presented to the user.

JOptionPane.showInputDialog

This static method is meant to ask the user a question, and get the user's answer.

Example:

import javax.swing.*;

class Test3 {
	public static void main(String args[]) {
		String s = JOptionPane.showInputDialog("hello world" );
		System.out.println("result: " + s);
		System.exit(0);
	}
}
when run, the program produces this message box:  

Explanation of parameters:

"hello world"
is the string that is displayed in the dialog box.
Notice that the showInputDialog method has a return value of type String. It contains whatever the user has typed in.
There are several more showConfirmDialog methods in the JOptionPane class. They have additional parameters to pre-fill the user's answer field with default text, to change the title of the dialog box, and to change the he question-mark symbol into others.

JOptionPane.showOptionDialog

This static method is meant to be a combination of the other 3 methods. It has a parameter that specifies whether the dialog is meant to be a "message", "confirm" or "input" dialog. All other parameters of the other methods can be specified. It can also be used for quite flexible input questions, where the user can select from a set of choices.

Example:

import javax.swing.*;

class Test4 {
	public static void main(String args[]) {
		Object []buttons = {"add","delete","move"};
		int i = JOptionPane.showOptionDialog(null,
			"Where do you want to go ?", "dialog title",
			JOptionPane.YES_NO_OPTION,
    		JOptionPane.QUESTION_MESSAGE,
    		null,
 			buttons, buttons[0]);
		System.out.println("result: " + i);
		System.exit(0);
	}
}
when run, the program produces this message box:  

Explanation of parameters:

null
means that there is no parent frame, that is the dialog is the only frame.
"Where do you ..."
is the string that is displayed in the dialog box.
"dialog title"
is the title of the dialog box.
JOptionPane.YES_NO_OPTION
button style.
JOptionPane.QUESTION_MESSAGE
indicates a question dialog.
null
not needed.
buttons
is an array of strings that appears on the buttons
buttons[0]
indicates the default selection.
Notice that the showOptionDialog method has a return value of type int. It returns index from the buttons array that the user has clicked.

More Information

Additional examples can be found in the
"How to Make Dialogs" chapter of the online Java Tutorial.
The source code of the 4 example Java programs is here.