Java 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 a lot of Customize version of the JOptionPane. we will discuss them in very details. 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 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. 

  • 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 message or error message. Let's have an Example for Better Understanding. 

JOptionPane.showMessageDialog(null,"ShowMessageDialog",
"title",JOptionPane.ERROR_MESSAGE,null);

The output of the 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. 

  • 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 string 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 option in this case. First, we can get string input from the user and the second is the user may cancel the dialogue box.
in the First case, we can assign the string that users his 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 in 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 is below

Java Swing

  • 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 will produce the following Java Swing Dialog Box.

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.

  • 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 a very customize versions of the dialogue boxes. So with our further explaining lets 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 lets first look into the code. if you see into the parameter list of the method we have a lot of Option provided to this method because this methods need more parameters
The first parameter is the parent Component which is null.
The second parameter is message which is a string value
The third parameter is the Option Types
The fourth parameter is the Message Type
The fifth parameter is the Text of the Button that we have passed a string array.
The last one is the initial value of the Button.

The output of the Above Code

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. 

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

  • JOptionPane.showInternalMessageDialog():-

All the Dilag windows that we have discussed so far were external. Now sometime we need to have some internal Dialog boxes this method in the JOptionPane Class gives us the facility to create internal 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. 
You might also like to read these articles

Post a Comment

Previous Post Next Post