JOptionPane is a Very Important Component in Java Swing Library. In the Previous Article, I have explained Different types of JOptionPane Dialog Boxes and their Properties. Things seem complex when until you see in the example. So I will try to demonstrate the Previous Article with the help of an example.
I will walk you through a simple Java Swing Program. We will create a simple Graphical User Interface with the Help of JOptionPane  Dialog Boxes. 

We will use the Methods of the JoptionPane Class that we have studied in the Previous Article( click here if you want to read it).  Not only that we will also discuss some important concepts to know about JoptionPane class.

First, see what we are gonna create by using the JOptioinPane class. So we will create Three Dialog Boxes. In the First and second Dialog box, the User will input some value and in the third dialogue box, the Program will show the Output to the User.
I will Write the Code and I will try to explain it to you. So let's try and write our code.

Source Code for this program with comments is following I will also discuss some main terminologies at the end of the Article.

package AlixaProDev;

import javax.swing.*;

// Class Name AdditionOfNo
// you can name it whatever you want
public class AdditionOfNo {
// lets write our code in the constructor

// to take the first input from the User
// we will use the JOptionPane InputDialog

// This method will take the Input from the User
// in the form of String so we need to declare a String
String firstNo;
// we will assign the value to fristNo;
firstNo=JOptionPane.showInputDialog("Enter First Number:");

// we will do the same thing for our second Dialog box
// 1. Declare a String variable
String secondNo;
// 2. Assign the value return from the user to secondNo;
secondNo = JOptionPane.showInputDialog("Enter Second Number:");

// Now these values firstNo and secondNo are in the form of String
// we need to convert it into Double datatype or Integer
// Double will be best
// Declare two variable of type Double
double n1= Double.parseDouble(firstNo);
double n2 = Double.parseDouble(secondNo);

// Okay now lets add these two Numbers
double sum = n1+n2;
// let us create another JoptionPane to show the Result
// the JoptionPane Message Dialog will be best for this Purpose
// we have used the Attribute JOptionPane.PLAIN_MESSAGE
// we will discuss this attribue at the end of the Article
// test that shoud be displayed is
String result = "Sum of Nos is: "+sum;

public static void main (String[] args) {
new AdditionOfNo();

let's look into the Output and then we will discuss some Important and final concepts related to the JoptionPance Class. 

Result of the Additon

So This was about the Addition of two Numbers with the Help of JOptoionPane class in Java Swing Library. I hope you can get all the concepts from the documented code that I have posted above the output.
Now there are some bonus tips while working with the JoptoinPane Class in Java Swing. so I will discuss them and hope you practice it. 

Bonus No 1:- 

Whatever the User Put into the JoptionPane Input Dialog will consider is a string and you have to convert it into the Suitable datatypes that you want to have. For Example in our case, we converted the String into Double Datatype. 

Bonus No 2:- 

Catch the Exception will doing the Conversion from String to other Datatypes like when you want to convert String into Integer, Double or Float you have to surround it with the Try Catch Block. 

Now let's discuss some of the constants that are related to the JOptionPane Message Dialog.

ErrorMessage In JOptionPane Message Dialog:- 

This constant is used when we want to show some error message. we can pass it to the method is parameter like below. 

JOptionPane.showMessageDialog(null, JOptionPane.ERROR_MESSAGE);

PlainMessage In JOptionPane Message Dialog:- 

This Constant is used to show a general message. By using this constant we do not have the option of the Image icon in the dialogue box.

So that's it. See you in the next Article. I hope you like this one. 

If you have any Question please let me know in the comment section. 

You may also like to read this.

Post a Comment

Previous Post Next Post