Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 6
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:
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:
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
WARNING_MESSAGE with OK_CANCEL_OPTION
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.
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 9
In this lesson we shall learn how to use the Switch statement in Java programming. Switch statement is a selection statement, that means when used, it select one value among many values. Switch statement can also be used instead of IF .. ELSE ...
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 8
IF .. ELSE statement has two parts, one to cater for when condition is TRUE and another for when condition is FALSE.
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 2
In this lesson you will learn how to create your first Java project and how to interpret the NetBeans code editor window.
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 5
One of the useful classes that handle inputs from a user in Java is the Scanner class. The Scanner class is located in the util (utility) package of the Java library.