JOptionPane Methods

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Calling Methods


Introduction

JOptionPane is a class, or toolbox, that allows you to use graphical windows for user input and output. We will be looking at showInputDialog for getting user input, showConfirmDialog for getting user confirmation, showMessageDialog for displaying messages, and showOptionDialog a combination of all three.

JOptionPane Methods

IMPORTANT: To use JOptionPane methods, you must (1) Be running your compiled program in a GUI based environment (i.e. Not Unix) and (2) The top of your .java file must contain the following line:

 import javax.swing.JOptionPane; 

You could also use .* which imports all of the files in javax.swing (including the JOptionPane files).

 import javax.swing.*; 

You have already seen JOptionPane being used for getting input from the user, but you can do more with JOptionPane. All JOptionPane windows have the same basic srtructure: an Icon, a message, input value(s) and option buttons (if applicable).

ShowInputDialog.PNG

In the picture the '?' is the question Icon, 'Please enter your name' is the message, The 'Default name' is in the input value, and 'OK', 'Cancel' are the option buttons. There are five options for the icon: none(plain), 'I' for info, '?' for questions, '!' for warnings, and 'X' for errors. The option buttons are a combination of 'Yes', 'No', 'OK', and Cancel'.


showInputDialog

There are two different basic ways of calling showInputDialog. You've already seen the first one, which is simply by calling the method and sending the message as an argument:

 String input;
input = JOptionPane.showInputDialog("Please enter input"); 

You may also set a default value for the input. After the message argument, add a comma and enter the default value in between quotation marks:

 String input;
input = JOptionPane.showInputDialog("Please enter a name", "Default Name"); 

The window will look like this:

ShowInputDialog.PNG


JOptionPane.showInputDialog is also useful for getting other data types. In the next example, the String that is returned by showInputDialog is converted into an int with the Integer class' parseInt method:

 String input;
int value;

input = JOptionPane.showInputDialog("Please enter a number");

// after getting an integer input value, it converts a string value into an integer value.
value = Integer.parseInt(input); 

If you want the user to enter a double instead of an int, replace Integer.parseInt in the above example with Double.parseDouble.

showConfirmDialog

JOptionPane.showConfirmDialog allows you to pop up a message window for the user to allow confirmation of user action. This is best used when the next action is dangerous or uncorrectable. The wording in the message portion should be clear and brief. As an example, "Once deleted you will not be able to use the file again. Click 'OK' to delete the file." There are four default option buttons that can be used in a confirmation dialog: 'OK', 'Yes', 'No', and 'Cancel'. However, you can reword these buttons if desired.

 // showConfirmDialog method returns an integer value based on user input.
int a = JOptionPane.showConfirmDialog(frame, "Are you sure you want to delete the file?",
"A Common Question", JOptionPane.YES_NO_OPTION); 

showMessageDialog

JOptionPane.showMessageDialog allows you to display a message window for the user:

 // this method displays a message.
JOptionPane.showMessageDialog(null, "Hello, world!"); 

Which results in:

ShowMessageDialog.PNG

Always use null as the first argument. The second argument is the String you would like to print out on the popup window.

showOptionDialog

Another usefull method in JOptionPane is the showOptionDialog method which combines showMessageDialog, showInputDialog and showConfirmDialog methods in to a single method.

 Object[] options = { "YES", "NO" };
 JOptionPane.showOptionDialog(null, "Click YES to continue", "Alert",JOptionPane.DEFAULT_OPTION,
 JOptionPane.WARNING_MESSAGE, null, options, options[0]); 

Example: Practice with showInputDialog and showMessageDialog

Here is a simple example showing how to use both the input and message dialogs to get a user's name and display it back to the user.

 // import JOptionPane library to use JOptionPane
import javax.swing.JOptionPane;

public class Practice
{
	public static void main(String[] args)
	{
		// declare a String type variable "input"
		String input;
		// get a user input
		input = JOptionPane.showInputDialog("Please enter your name");
		// display what a user typed on the message dialog box (GUI)
		JOptionPane.showMessageDialog(null, "Hello " + input + " !!");
		// display what a user typed on a regular output(Command Output)
		System.out.println("My name is " + input);
	}
} 

Which results in if you type "Ryoji" as your input:

Output

OutputRyoji.PNG
 My name is Ryoji 

More Information on JOptionPane

For more information on JOptionPane visit the [JOptionPane API].

Summary

In this section we have learned how to use JOptionPane methods to make use of GUI features available in Java. We have also learned how we can do simple user input and output with the help of GUI. We also spent some time looking at various methods in GUI such as showInputDialog(), showConfirmDialog(), showMessageDialog() and showOptionDialog(). This is the end of the Calling Methods chapter. Take some time now and see if you can do some practice questions.

Previous Page: Math Methods Next Page: Calling Methods