JOptionPane Methods

From CompSciWiki
Revision as of 21:05, 7 December 2011 by CharuC (Talk | contribs)

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:

code

 import javax.swing.JOptionPane; 

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

code

 import javax.swing.*; 

You have already seen JOptionPane being used for getting input from the user here, 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:

code

 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:

code

 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:

code

 String input;
int value;

input = JOptionPane.showInputDialog("Please enter a number");
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.

code

 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 pop up a message window for the user:

code

 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.

code

 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

code

 // 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

Output

 C:\Users\umbetcha\Java>java Practice
My name is Ryoji 

More Information on JOptionPane

For more information on JOptionPane visit the Sun website: http://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.html

Summary

In this section we learnt how to use JOptionPane to make use of GUI features available in Java. We learnt how we can do simple user input and output with the help of GUI. We learnt to use various methods in GUI such as showInputDialog(), showConfirmDialog(), showMessageDialog() and showOptionDialog().

Previous Page: Math Methods Next Page: Calling Methods