Saturday, June 29, 2019

Types and Use of JOptionPane in Java Swing for Messages in NetBeans

Types and Use of JOptionPane in Java Swing for Messages in NetBeans

In java, if you want to show message box, you have to use jOptionPane class to handle message dialog box. If you know, how to show Message box in vb or Then you will not face any problem in java, basically every programming language contains some same things in coding.

So, in this post SKOTechLearn will describe the process of showing different types of messages through  jOptionPane  in java  Swing NetBeans.

What is jOptionPane in Java?

It is simply a message box in which you can define a message and show in different types as we learn in this post. 

There is following types of Msg dialog appearance that we will learn:

 (1). Show simple Plain MsgBox
 (2). Msgbox with Custom image icon dialog
 (3). MsgBox in Yes/No option with image dialog
 (4). Error MsgBox dialog
 (5). Information MsgBox dialog

So, let's find step by step every message process.

Now first we learn how to show simple MsgBox dialog.

First, we drag some jButton for every MsgBox. And will write code for it, then see what happen in every different jbutton's ActionPerformed event's process.

 (1). Show simple jOptionPane Msg dialog :
For showing this dialog, you have to simply import its class. Then call it in any component or run time process.
import javax.swing.JOptionPane; 
If there is only simple Msg dialog requirement, this is called plain MsgBox. We want to show it through butons’s click activity. So, type following code as we written.
private void MsgBtnActionPerformed(java.awt.event.ActionEvent evt) {
       JOptionPane.showMessageDialog(null, "Basic Plain Msg", "Plain MsgBox", JOptionPane.PLAIN_MESSAGE);
When you run it, it will show the following output.
Plain Message box in Netbeans
Plain Msg dialogbox
 (2). jOptionPane with Custom image icon dialog :
Suppose you have an image icon, which you want to show with containing message box, which indicate your application process. Then create an image icon and save it as .png or .jpg format and drag it to your package.
Suppose we have a file with name "msgicon.png" Message Icon in java . Then write code like:
private void ImgMsgBtnActionPerformed(java.awt.event.ActionEvent evt) { 
        BufferedImage imageicn1 ="msgicon.png"));
        ImageIcon iconsk = new ImageIcon(imageicn1);
        JOptionPane.showConfirmDialog(null, "Custome Image Base Msg", "Msg with Image icon", JOptionPane.PLAIN_MESSAGE, 3, iconsk);
  }catch(Exception msex1){}
After that this code will present a custom icon base msg.
Custom Image base Message box in Netbeans
Custom Image Base Msg DialogBox

SQL Server Database Connectivity in Netbeans with Easy Tech Tips

 (3). jOptionPane with Yes No option with image dialog :
For saving or editing or some other action you need a MsgBox that show Yes/No option for that process. You have to add if else statement for it.

For clear description we add some jLabel and will see the output of Yes/No button press, which show in jLabel.

For more understanding purpose, just look at following code:
private void YesNoMsgBtnActionPerformed(java.awt.event.ActionEvent evt) {
   BufferedImage imageicn1 ="msgicon.png"));
        ImageIcon iconsk = new ImageIcon(imageicn1);
  //Condition if you press “Yess” Button.
          if (JOptionPane.showConfirmDialog(null, "Sure for this process", "Ye No Option",  JOptionPane.YES_NO_OPTION,3, iconsk) == JOptionPane.YES_OPTION) {
    //Otherwise if you press “No” Button.
     else {
  }catch(Exception msgl){} 
And run-time this will show like:
YesNoOption Msg Box in Netbeans
Yes/No Msg Dialog
As you can see when pressing on “Yes” button, the result is showing in jLabel2 as “Yes” and pressing on “No” button, the result is showing in jLabel2 as “No”.

jList to Display data or Items in Java Swing

 (4). jOptionPane with error dialog :
If you want to show an error Msgbox, there is simple code for it.
private void ErrMsgBtnActionPerformed(java.awt.event.ActionEvent evt) { 
   JOptionPane.showMessageDialog(null, "Sorry Activity Fail", "Activity Fail Error" , JOptionPane.ERROR_MESSAGE);
Error Msg dialog in Netbeans
Error Dialog Box
 (5). jOptionPane with information dialog :
For successful saving, editing, deleting and listing information, you need to show the information MsgBox. Just change some code inside error dialog. And the Msg dialog will be change in information dialog.
private void InfoMsgBtnActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null, "Successfully Done Activity", "Activity Done Information" , JOptionPane.INFORMATION_MESSAGE);
information Message box in java
Information Dialog Box
So, there is some Types and Use of JOptionPane in Java Swing for Messages in NetBeans as described above with step by step process, just do same as SKOTechLearn Tips mention above.


  1. Such a really helpful article. terribly fascinating to scan this text.I would wish to thanks for the efforts you had created for putting this on ink awe-inspiring article.