cstutoringcenter.com logo

E-mail:    Password:    

Become a premier member today to gain access to exclusive member benefits! Just $10.00 to join FOR LIFE!

Click Here

<< Tutorial 5
Tutorial 7 >>
Java tutorial index

JOptionPanes for Java Input/Output

Tutorial 6

Register or log in today to rate this tutorial!
Current ranking: / 5 stars with 22 votes.

What is a JOptionPane?
Wrapper classes
Input uses
Output uses
Example 1
Example 2

What is a JOptionPane?

A JOptionPane (spelled as is) is part of the java swing library. It requires an import statement at the top of the program. Here is what the statement is:

import javax.swing.JOptionPane;
import javax.swing.*;

Either of those is acceptable. The difference is that the latter will import the entire library as denoted by the star. The first will just import the JOptionPane.

The pane will allow a user to do a few things. The user can enter data, display messages or a combination of those.

Wrapper Classes

A JOptionPane can be used to input numbers or strings. By default, anything entered in a JOptionPane is stored as a string in Java. To fix this, we need what is called a wrapper class. This allows us to convert the string into the desired number data type (int, float, double, long or short). Here are the wrapper class names:

   -for the int type
   -for the double type
   -for the float type
   -for the short type
   -for the long type
   -for the char type
   -for the boolean type
   -for the void type
   -for the byte type

A very important thing to observe is that the names of these wrapper classes are all capital letters. This is how they are differentiated from some data types.

With the exception of the Character class, each of the above has a method called parseXXX where the XXX is the primitive type name (int, float, double, short, long or boolean). This method will actually change the values into the correct type. Let's see a code snippet:

int x = Integer.parseInt(args[0]);

double d = Double.parseDouble(args[1]);


So what are the two methods for a JOptionPane you need? Here they are. The first is used for input purpose and the second is used for output.

To use a JOptionPane for input, you use the following method:


where in the above, JOptionPane is required to access the methods, null is a keyword representing nothing, and the [message] is the appropriate message that will be displayed in the JOptionPane dialog window. The above will be used inside of a parseXXX method if you are trying to input a numeric type.

JOptionPane for Input

Here is an example showing a dialog box for input and then displaying the results to the results to another JOptionPane. The below program will ask for the users name and age.

Example 1:
JOptionPane input example

Download source code here (Right click - Save Target As...)

import javax.swing.JOptionPane;
public class InputExample{
	public static void main(String[] args){
		String name = "";
		int age = 0;
		name = JOptionPane.showInputDialog(
                        null, "Please enter your name: ");

                age = Integer.parseInt(
                             null, "Please enter your  age: "));
	        System.out.println("Hello " + name + ". You are " 
                        + age + " years old!");
	} //main
} //class

Messy code to look at indeed! However, it is not too bad after all. Firstly, the program will prompt the user for the name first by showing a JOptionPane input dialog. The program will pause and wait for the user to enter the data.

It will then show another box for the user to enter their age. It needs to parse the number to an integer as all data entered in a JOptionPane are stored as Strings.

Finally, a message box is shown displaying the data that was entered by the user.

No command line arguments are needed as the input is coming from the input dialog. The output goes to the console via the println statement.

But what if we wanted the input/output to be done strictly on JOptionPanes? We can do that as well.

JOptionPane for Output

To use a JOptionPane for output, use the following statement:


Here is a program that will average three numbers and display that average to a JOptionPane.

Example 2:
JOptionPane output example

Download source code here (Right click - Save Target As...)

import javax.swing.JOptionPane;
public class OutputExample{
	public static void main(String[] args){
		int num1, num2, num3;
		double avg = 0.0;
        	num1 = 4.9;
                num2 = 9.1;
                num3 = 12;

	        avg = (num1+num2+num3)/3.0;
                JOptionPane.showMessageDialog(null, "The average is: " + avg);	
       } //main
} //class

This time, there is no data entry being preformed. It simply displays a message on the JOptionPane window.

Example 3:
JOptionPane input/output example

Download source code here (Right click - Save Target As...)

import javax.swing.JOptionPane;
public class IOExample{
   public static void main(String[] args){
      double nums[] = new double[5];
      for(int i = 0; i < 5; i++){
         //get number from input dialog
         String str = JOptionPane.showInputDialog(null, "Enter number " + (i+1) + ": ");

         //convert that number to a double value
         nums[i] = Double.parseDouble(str);

      //find the mystery value
      double mystery = nums[0] * nums[2] + nums[4];

      //display message box
      JOptionPane.showMessageDialog(null, "The mystery value is: " + mystery);

   } //main
} //class

The above program will ask the user for 5 double value numbers. There will be a total of 5 input dialog boxes appearing when the loop is running. It then converts each number entered with the Double wrapper class. Finally, the mystery value is calculated and displayed to the user via a message dialog box.

<< Tutorial 5
Tutorial 7 >>
Java tutorial index


Please feel free to leave a comment! Just keep it peaceful is all we ask and try to be as informative as possible to help others. Spam will NOT be tolerated!

Reviewing C++

Reviewing C++

Print: $39.99

Reviewing Java

Reviewing Java

Print: $34.99