Difference between revisions of "Input using JOptionPane"

From CompSciWiki
Jump to: navigation, search
(Chapter TOC)
(Doug's edits)
Line 1: Line 1:
{{1010Topic|Introduction=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.|Overview=Learn how to use input boxes to get input from the user.|Chapter_TOC=[[Your First Java Program]]}}
+
{{1010Topic|Introduction=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 a string from a user.|Overview=Learn how to use input boxes to get a string from the user.|Chapter_TOC=[[Your First Java Program]]}}
 
==Using an Input Box to Get Input From the User==
 
==Using an Input Box to Get Input From the User==
 +
 
===Example===
 
===Example===
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.
+
In this example we prompt the user to enter their name, store it in a [[Variables_and_Literals|variable]], and greet them with their name using System.out.
  
 
<pre>
 
<pre>
Line 20: Line 21:
 
</pre>
 
</pre>
  
 
+
[[Image:input1.png|right]]
We will now describe the important lines in this program.
+
We will now describe the important lines in this program:
 
+
  
 
*import javax.swing.JOptionPane;
 
*import javax.swing.JOptionPane;
JOptionPane is part of the javax.swing package. To give our class access to JOptionPane we have to use the import statement.
+
'''JOptionPane''' is part of the '''javax.swing''' package. To give our class access to JOptionPane we have to use the import statement.
 
+
  
 
*name = JOptionPane.showInputDialog("Enter your name:");
 
*name = JOptionPane.showInputDialog("Enter your name:");
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.
+
The method '''showInputDialog''' pops up a dialog (pictured to the right) 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]]
+
  
  
 
*System.out.println("Hello " + name);
 
*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.
+
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.

Revision as of 21:16, 21 March 2007

COMP 1010 Home > Your First Java Program


Introduction

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 a string from a user.

   

{{{Body}}}

Using an Input Box to Get Input From the User

Example

In this example we prompt the user to enter their name, store it in a variable, and greet them with their name using System.out.

import javax.swing.JOptionPane;

public class Input
{
  public static void main(String args[]) 
  {
    String name;
    
    name = JOptionPane.showInputDialog("Enter your name:");

    System.out.println("Hello " + name);
  }
}
Input1.png

We will now describe the important lines in this program:

  • import javax.swing.JOptionPane;

JOptionPane is part of the javax.swing package. To give our class access to JOptionPane we have to use the import statement.

  • name = JOptionPane.showInputDialog("Enter your name:");

The method showInputDialog pops up a dialog (pictured to the right) 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.


  • 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.