Java GUI (Swing) | JOptionPane

JOptionPane is a very important Java Swing GUI Component which is used to provide the User a pre-built
Dialogue boxes to Enter some text or select from the Options. These are very Important Components In Java.

Today we will be discussing the important properties of the OptionPane and some Important Features and Types of the JOptionPane.

There are a lot of Customize versions of the JOptionPane. we will discuss them in very detail. The Look and feel of these JOptionPane can be a little Different by using the attributes and different methods that we can use. When it comes to the method of the JOptionPane Class in Java Swing the Classical Graphical User Interface (GUI) Framework we have 4 Static Method. I have mentioned this if someone asks you

How many DialogBoxes are there in the JOptionPane class in Java?

Then your answer should be four. Although we can customize these Dialog boxes the way you want.

Now we will discuss some details about these methods and Dialog Boxes that are produced by these Methods.

  1. JOptionPane.showMessageDialog():-

    This is the most common method of the JOptionPane Class in Java Swing. Depending on the Number of Parameters that I will Explain it gives us Different Results. We will look into a few cases and we will show the Attributes values as well.

    package AlixaProDev;
    import javax.swing.*;
    public class JOptionDialogDemo {
    JOptionDialogDemo(){
    JOptionPane.showMessageDialog(null,"ShowMessageDialog");
    }
    public static void main (String[] args) {
    new JOptionDialogDemo();
    }
    }
    The outPut of the Above Code.
    JOptionPane.ShowMessageDialog()

    Okay Now, this was the Expected Output of that Method. This Static Metod of Java Swing takes at least two Parmeter. if you can see in the output we have a text "showMessageDialog" this is the message that we want to Display and this is the Second Parameter of the JOptionPane.showMessageDialog(null, message). The first parameter is the Parent Layout on which we want to display our Message Box. in our case we do not have any Parent Swing Component so we put null as an attribute value.

    The other thing that you can notice is the "ok" button. We have only one Option this time but we can specify how many buttons we want to display by using the other static method this method has only one option because it is useful for showing information messages or error messages. Let's have an Example for Better Understanding. 

    JOptionPane.showMessageDialog(null,"ShowMessageDialog",
    "title",JOptionPane.ERROR_MESSAGE,null);
    The outPut of the Above Code.
    Error Message In Java Swing

    This time we have the title of the Dialog Box "title" and we have a different icon by default it is of the information type icon. but this time this icon is useful for showing an error. we can also create our custom icon and put it in there is an attribute value.

    Remember:- This method is very useful when we want to show some information message or we want to an Error Message. 

  2. JOptionPane.showInputDialog():-

    This is another Static Method of Java Swing JOptionPane Class. With this method, we can take input from the user. The user will input some String and we can assign these strings into a String variable. This method has many overloaded versions and we can change the look and feel of this Dialog Box as well. we will check the most common one here.

    package AlixaProDev;
    import javax.swing.*;
    public class JOptionDialogDemo {
    JOptionDialogDemo(){
    JOptionPane.showInputDialog("Input Text Here:");
    }
    public static void main (String[] args) {
    new JOptionDialogDemo();
    }
    }

    The output of the above code looks like this.

    JOptionPane.showInputDialog()

    we only have two options in this case. First, we can get string input from the user and the second is the user may cancel the dialogue box.
    in In the First case, we can assign the string that users inputted into a String variable like this.

    String text = JOptionPane.showInputDialog("Input Text Here:");

    Once the user input some text and press the okay button the String variable "text" will be assigned by that string. if the user presses the cancel button the value of the String variable will be null.

    we can check it by outputting the values of the string variable. 

    Now as I have mentioned before we can specify the title and the icon of this Dialog box. 

    The second parameter that this method can take is the initial or the default value in the text field. we can specify it by passing the second parameter to the method is below. in the below statement I have passed some text is the default string that this dialogue box will have. We will see that in the output. 

    String text = JOptionPane.showInputDialog("Input Text Here:",
    "There goes some text");
    The outPut of the Above Code.
    Java Swing

  3. JOptionPane.showConfirmDialog():-

    This method will produce a dialogue box that will have buttons. The button in the confirmation box may have different values. Like they can be yes, No, cancel, Ok and others. These buttons have some values when they are pressed they produce some integer constants that we can use and based on these Integer values, we can make a decision.

    package AlixaProDev;
    import javax.swing.*;
    public class JOptionDialogDemo {
    JOptionDialogDemo(){
    int decisionNo = JOptionPane.showConfirmDialog(null,
    "Are you Sure");
    System.out.println(decisionNo);
    }
    public static void main (String[] args) {
    new JOptionDialogDemo();
    }
    }
    The outPut of the Above Code.

    The Above code will Produce 0 if the user press on Yes, 1 if the User Press on the No and 2 if the user press on the Cancel button.

  4. JOptionPane.showOptionDialog():-

    This is the Last but the Most Powerful and very Useful Dialog box in the JOptionPane class in swing Java. you will most of the time using this method in order to produce very customize versions of the dialogue boxes. So with our further explaining let's create one to see how it will look like.

    package AlixaProDev;
    import javax.swing.*;
    public class JOptionDialogDemo {
    JOptionDialogDemo(){
    String [] optText={"okay Got it", "No Alixa"};
    int decisionNo = JOptionPane.showOptionDialog(null,
    "Option Box","title goes here",
    JOptionPane.YES_NO_CANCEL_OPTION,
    JOptionPane.QUESTION_MESSAGE,null,optText,0);
    System.out.println(decisionNo);
    }
    public static void main (String[] args) {
    new JOptionDialogDemo();
    }
    }

    Before Looking into the output let's first look into the code. if you see into the parameter list of the method we have a lot of Options provided to this method because these methods need more parameters

    1. The first parameter is the parent Component which is null.
    2. The second the parameter is the message which is a string value
    3. The third parameter is the Option Types
    4. The fourth parameter is the Message Type
    5. The fifth parameter is the Text of the Button that we have passed a string array.
    6. The last one is the initial value of the Button.
    The outPut of the Above Code.

    Besides all of the above, we do have some important methods in JOptionPane class. Some of them I will discuss here.

  5. JOptionPane.showInternalMessageDialog():-

    All the Dilag windows that we have discussed so far were external. Now sometimes we need to have some internal Dialog boxes this method in the JOptionPane Class gives us the facility to create internally Message boxes as well. These are very much similar to the above. we also have other internal dialogues like the above we have explained they are very much similar and I wish you can have a look at them on your own. 

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

AdditionOfNo(){
// 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;
JOptionPane.showMessageDialog(null,result,
"sum",JOptionPane.PLAIN_MESSAGE);

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

Summary and Conclusion:-

So this was all about the JOptionPane Class Messages.if you have any questions please let me know in the Comment Section I will be happy to answer them.

Post a Comment

0 Comments