Difference between revisions of "JOptionPane Methods"

From CompSciWiki
Jump to: navigation, search
 
(11 intermediate revisions by 4 users not shown)
Line 10: Line 10:
 
==JOptionPane Methods==
 
==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:
 
'''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===
+
 
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
Line 17: Line 17:
  
 
You could also use .* which imports all of the files in javax.swing (including the JOptionPane files).
 
You could also use .* which imports all of the files in javax.swing (including the JOptionPane files).
===code===
+
 
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
Line 23: Line 23:
 
}}
 
}}
  
You have already seen JOptionPane being used for getting input from the user [[Input using JOptionPane|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).
+
You have already seen [[Input using JOptionPane|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).
  
[[Image:showInputDialog.PNG]]
+
[[Image:showInputDialog.PNG|center]]
  
 
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'.
 
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()===
+
===<code>showInputDialog</code>===
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:
+
There are two different basic ways of calling <code>showInputDialog</code>. You've already seen the first one, which is simply by calling the method and sending the message as an argument:
  
===code===
 
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
Line 42: Line 41:
 
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:
 
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===
 
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
Line 51: Line 49:
 
The window will look like this:
 
The window will look like this:
  
[[Image:showInputDialog.PNG]]
+
[[Image:showInputDialog.PNG|center]]
  
  
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>JOptionPane.showInputDialog</code> 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===
 
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
Line 63: Line 60:
  
 
input = JOptionPane.showInputDialog("Please enter a number");
 
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);
 
value = Integer.parseInt(input);
 
}}
 
}}
Line 68: Line 67:
 
If you want the user to enter a double instead of an int, replace '''Integer.parseInt''' in the above example with '''Double.parseDouble'''.
 
If you want the user to enter a double instead of an int, replace '''Integer.parseInt''' in the above example with '''Double.parseDouble'''.
  
===showConfirmDialog()===
+
===<code>showConfirmDialog</code>===
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>JOptionPane.showConfirmDialog</code> 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===
 
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
 +
// showConfirmDialog method returns an integer value based on user input.
 
int a = JOptionPane.showConfirmDialog(frame, "Are you sure you want to delete the file?",
 
int a = JOptionPane.showConfirmDialog(frame, "Are you sure you want to delete the file?",
 
"A Common Question", JOptionPane.YES_NO_OPTION);
 
"A Common Question", JOptionPane.YES_NO_OPTION);
 
}}
 
}}
 
   
 
   
===showMessageDialog()===  
+
===<code>showMessageDialog</code>===  
 +
 
 +
<code>JOptionPane.showMessageDialog</code> allows you to display a message window for the user:
  
JOptionPane.showMessageDialog allows you to pop up a message window for the user:
 
===code===
 
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
 +
// this method displays a message.
 
JOptionPane.showMessageDialog(null, "Hello, world!");
 
JOptionPane.showMessageDialog(null, "Hello, world!");
 
}}
 
}}
Line 89: Line 89:
 
Which results in:
 
Which results in:
  
[[image:ShowMessageDialog.PNG]]
+
[[Image:ShowMessageDialog.PNG|center]]
  
 
Always use null as the first argument. The second argument is the String you would like to print out on the popup window.
 
Always use null as the first argument. The second argument is the String you would like to print out on the popup window.
  
===showOptionDialog()===
+
===<code>showOptionDialog</code>===
 +
 
 +
Another usefull method in <code>JOptionPane</code> is the <code>showOptionDialog</code> method which combines <code>showMessageDialog, showInputDialog</code> and <code>showConfirmDialog</code> methods in to a single method.
  
Another usefull method in JOptionPane is the showOptionDialog method which combines showMessageDialog, showInputDialog and showConfirmDialog methods in to a single method.
 
===code===
 
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
Line 106: Line 106:
 
}}
 
}}
  
===Example "Practice" with showInputDialog and showMessageDialog===
+
===Example: Practice with showInputDialog and showMessageDialog===
===code===
+
 
 +
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.
 +
 
 
{{CodeBlock
 
{{CodeBlock
 
|Code=
 
|Code=
Line 132: Line 134:
 
Which results in if you type "Ryoji" as your input:  
 
Which results in if you type "Ryoji" as your input:  
 
===Output===
 
===Output===
[[Image:OutputRyoji.PNG]]
+
[[Image:OutputRyoji.PNG|center]]
 
+
====Output====
+
  
 
{{OutputBlock
 
{{OutputBlock
 
|Code=
 
|Code=
C:\Users\umbetcha\Java>java Practice
 
 
My name is Ryoji
 
My name is Ryoji
 
}}
 
}}
 
===More Information on JOptionPane===
 
===More Information on JOptionPane===
  
For more information on JOptionPane visit the Sun website:
+
For more information on JOptionPane visit the [[http://docs.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html JOptionPane API]].
http://java.sun.com/javase/6/docs/api/javax/swing/JOptionPane.html
+
  
 +
==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 [[Calling Methods Questions|practice questions]].
 
}}
 
}}

Latest revision as of 15:43, 18 January 2012

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