Difference between revisions of "Input using JOptionPane"

From CompSciWiki
Jump to: navigation, search
m
m (Removed <code> tags)
 
(36 intermediate revisions by 6 users not shown)
Line 1: Line 1:
JOptionPane gives you the ability to create dialogs. A dialog is a window that pops up and either displays a message or requests input from a user. This section will show you how to use JOptionPane to get input from a user.
+
{{Template:1010Topic
 +
|Chapter_TOC=[[Java Fundamentals]]
 +
|Previous=[[Output using JOptionPane]]
 +
|Next=[[Input using Scanner]]
 +
|Body=
  
==Input Box==
+
==Introduction==
===Example===
+
You've used [[Output using JOptionPane|JOptionPane]] to output a message using JOptionPane.showMessageDialog. Now let's focus on using JOptionPane to get information from the user, using JOptionPane.showInputDialog.  
In this example we prompt the user to enter their name, store it in a variable (see section on variables), and print their name to the screen using System.out.
+
  
<pre>
+
Also, remember JOptionPane.showInputDialog also requires the import statement <tt>import javax.swing.JOptionPane; </tt>at the top of your program.
 +
 
 +
==showInputDialog==
 +
An input dialog requests information from the user and stores it for use in the program.
 +
===Code===
 +
{{CodeBlock
 +
|Code=
 
import javax.swing.JOptionPane;
 
import javax.swing.JOptionPane;
 
+
public class InputDialog {
public class Input
+
public static void main(String args[]){
{
+
String message; // This will hold the result of your input.
  public static void main(String args[])  
+
message = JOptionPane.showInputDialog("Enter your name");
  {
+
}
    String name;
+
   
+
    name = JOptionPane.showInputDialog("Enter your name:");
+
 
+
    System.out.println("Hello " + name);
+
  }
+
 
}
 
}
</pre>
+
}}
 
+
 
+
We will now describe the important lines in this program.
+
 
+
  
*import javax.swing.JOptionPane;
+
===Output===
JOptionPane is part of the javax.swing package. To give our class access to JOptionPane we have to use the import statement.
+
[[Image:ShowInputDialog.png|center]]
  
 +
===How It Works===
 +
The <code>showInputDialog("Enter your name")</code> method creates a dialog with the message "Enter your name" and provides a text box for users to enter input. The contents of the text box is returned as a [[Strings|String]] after the user presses OK. The [[Strings|String]] is stored in the variable <code>message</code> in the code example above.
  
*name = JOptionPane.showInputDialog("Enter your name:");
+
At this point you don't know much about [[Variables and Literals|variables]]. That will change quickly! But for now just think of a [[Variables and Literals|variables]] of a place in the computer's memory where you can store a value. It is significant that the [[Variables and Literals|variables]] <code>message</code> has the word [[Strings|String]] in front of it. That states that <code>message</code> will store a [[Strings|String]] value. This works well, since JOptionPane.showInputDialog will store the user's input in the form of a [[Strings|String]].  
The method on the right of the equals sign pops up a dialog with the message "Enter your name:" and an input box for the user to enter their name. Once the user enters their name and hits the OK button, the method returns with the string that was entered in the input box. This string is stored in the name variable.
+
  
[[Image:input1.png]]
 
  
 +
==Summary==
 +
In this section you have learned how to collect user information using <code>JOptionPane</code>.  At this point you can input, output, and so you can write some more interesting programs. In the next section you will learn how to get user input from the command line.
  
*System.out.println("Hello " + name);
+
}}
Now that we have stored the user's name, we can use System.out.println to print "Hello name", where name is what the user entered in the input box.
+

Latest revision as of 12:42, 27 March 2012

COMP 1010 Home > Java Fundamentals


Introduction

You've used JOptionPane to output a message using JOptionPane.showMessageDialog. Now let's focus on using JOptionPane to get information from the user, using JOptionPane.showInputDialog.

Also, remember JOptionPane.showInputDialog also requires the import statement import javax.swing.JOptionPane; at the top of your program.

showInputDialog

An input dialog requests information from the user and stores it for use in the program.

Code

 import javax.swing.JOptionPane;
public class InputDialog {
	public static void main(String args[]){
		String message; // This will hold the result of your input.
		message = JOptionPane.showInputDialog("Enter your name");
	}
} 

Output

ShowInputDialog.png

How It Works

The showInputDialog("Enter your name") method creates a dialog with the message "Enter your name" and provides a text box for users to enter input. The contents of the text box is returned as a String after the user presses OK. The String is stored in the variable message in the code example above.

At this point you don't know much about variables. That will change quickly! But for now just think of a variables of a place in the computer's memory where you can store a value. It is significant that the variables message has the word String in front of it. That states that message will store a String value. This works well, since JOptionPane.showInputDialog will store the user's input in the form of a String.


Summary

In this section you have learned how to collect user information using JOptionPane. At this point you can input, output, and so you can write some more interesting programs. In the next section you will learn how to get user input from the command line.

Previous Page: Output using JOptionPane Next Page: Input using Scanner