MessageBox in Java and Use of JOptionPane in Java Swing NetBeans

This post describe about the MessageBox of java, If you want to show message box, you have to use jOptionPane class to handle message dialog box. you will find many customization option in jOptionPane in Java according to requirement. You can simply choose what type of message Dialog you want to show in your project.

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 are following types of jOptionPane dialog appearance that we will learn:

  1. Show simple Plain MessageBox in Java
  2. jOptionPane with Custom image icon dialog
  3. jOptionPane Yes/No option with image dialog
  4. jOptionPane Error Message dialog
  5. jOptionPane Information Message dialog

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

Now first we learn how to show simple MsgBox dialog.

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

(1). Show simple Plain MessageBox in Java

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) {
  try{
        BufferedImage imageicn1 = ImageIO.read(getClass().getResource("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 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) {
try{
    
      BufferedImage imageicn1 = ImageIO.read(getClass().getResource("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) {
          jLabel2.setText("Yes");
          jLabel2.setForeground(Color.blue);
       } 
       //Otherwise if you press “No” Button.
     else {
       jLabel2.setText("No");
       jLabel2.setForeground(Color.red);
      }
 
  }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 Error Message 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 Information Message 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.

2 comments:

  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.

    ReplyDelete