ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

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

Updated on October 16, 2019
dwachira profile image

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

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


    0 of 8192 characters used
    Post Comment
    • dwachira profile imageAUTHOR

      Danson Wachira 

      7 years ago from Nairobi, Kenya

      Hi ANU,

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

    • profile image


      7 years ago


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

    • dwachira profile imageAUTHOR

      Danson Wachira 

      7 years ago from Nairobi, Kenya

      Hi Gman,

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

    • profile image


      7 years ago

      Can you the code for WARNING_MESSAGE with OK_CANCEL_OPTION please?

    • dwachira profile imageAUTHOR

      Danson Wachira 

      7 years ago from Nairobi, Kenya

      Hi Lo Kay,

      Welcome and thank you for the visit and comment.

    • profile image

      Lo Kay 

      7 years ago

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

    • profile image


      7 years ago

      Trust God with all your heart!


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)