Skip to main content

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 6

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies

Table of Contents

<< Lesson 5 | Lesson 7 >>


Lesson 6: Working with Java Option Panes


Over the previous lessons, we have been using a console window as our output window. In Lesson 5, we saw how we can use Scanner class to get inputs from the user.

In this lesson, we’ll learn about another important class called JOptionPane of the javax.swing library.

This class contains dialogs that can be used to request information from the user, display information or give the user a choice e.g. “OK” and “Cancel”, “Yes” and “No” etc. A dialog box is a small graphical window that displays a message to the user or requests input.

The class JOptionPane contains input boxes and dialogs as shown below:


With JOptionPane class you can create customized dialogs that suit your needs in programming. JOptionPane provides support for layout on standard dialogs by providing icons, specifying the dialog title and text, and you can also customize the text contained in the command button.

The following image shows some of the icons that can be used with JOptionPane class compared to the equivalent in Windows.


To include JOption class, the following line of code must be included in your program just after the package name:

import javax.swing.JOptionPane;

The above line of code lets Java know that we are going to make use of JOptionPane class in the javax.swing library.

For the purpose of this lesson, create a new class, call it Jpane. Once you have created the class, add the above statement. Your code editor should look like shown below:


Just like we did in Lesson 5, we are going to get an input from the user and display the same, but this time we’ll use JOptionPane class methods. We are going to ask the user a question "What is a 'string' in Java?" and display the response entered by the user using a dialog box.

We’ll get an input from the user and store it into a variable called response which will be of string data type. We’ll use one of the JOptionPane class methods called showInputDialog. So, add the following lines of code in your “main” method:

String response;

response = JOptionPane.showInputDialog("What is a 'string' in Java?") //Type as one line

Scroll to Continue

To display the response that the user has entered, we’ll use a message box. This is yet another method in the JOptionPane class. Add the following statement in your code.

JOptionPane.showMessageDialog( null, response);

The keyword null that will appear inside the brackets denotes that the message box is not associated with any other output of our program. In between the brackets after the keyword null, type response.This is the name of the variable that contains the output we want to display. Because we have created several objects in our code, once we run the program we are going to clear them. So, add the following statement in your code.


Your code editor window should look like shown below:


Run the program by right-clicking anywhere inside of the code editor window and selecting Run File. You will see the following input box.


Enter response of your choice and click “OK


After clicking “OK”, you should be able to see your response being displayed on a message dialog box.


Dialog boxes can be formatted to have a meaningful feel and look icon as shown by the following code:

showMessageDialog(null, response, "Answer", JOptionPane.INFORMATION_MESSAGE); //Type as one line.

The bold part will make the dialog box to have an informational feel and look icon. Other options of feel and look icons that you may try include:

Message dialogs: Feel and look icons


For option dialogs, you can define the set of option buttons that appear at the bottom of the dialog box.

Option dialogs: Button options






Input dialog boxes can also work with numbers but because they take in text only you need to convert strings into numbers. To convert strings into numbers, we use the method Integer.parseInt( text to convert ). Note inside the brackets we put the string we want to convert.

Let us write a program that takes in two inputs (length and width) from the user and multiply them to display the area. To do this, you’ll need three variables; length, width and area. Two of them, length and width, will be of String type but we’ll later convert them into int type before we do multiplication.

Create another class called FindArea and type the following code:


Notice how we have converted length and width from string to int before multiplying them and storing the result into area variable.

Run the program, enter length then click “OK”, enter width click “OK”. You should be able to see an output as your area in a message dialog box.


Ok, that is all for Java Option Panes for now, in the next lesson we shall cover Control Structures in Java.

<< Lesson 5 | Lesson 7 >>

Want to make money online?


Danson Wachira (author) from Nairobi, Kenya on April 24, 2013:


Glad that this tutorial is helping you. Thanks for the visit and comment.

ANU on April 23, 2013:


Ur tutorials help me a lot as I am a teacher and I follow ur method in teaching my students...thanks a lot!

Danson Wachira (author) from Nairobi, Kenya on March 10, 2013:

Hi Gman,

I have already provided one example up there, just check. Thanks for the visit and comment.

Gman on March 09, 2013:

Can you the code for WARNING_MESSAGE with OK_CANCEL_OPTION please?

Danson Wachira (author) from Nairobi, Kenya on February 21, 2013:

Hi Lo Kay,

Welcome and thank you for the visit and comment.

Lo Kay on February 21, 2013:

Thank you for this tutorial, it has helped a lot. So far this is one of the best tutorials on the net

edudzi on September 27, 2012:

Trust God with all your heart!

Related Articles