This tutorial lets you a clear understanding of Swing JOptionPane. The JOptionPane is used to generate a standard pop up or dialogue boxes that prompts the users for taking values or inform something.

Swing JOptionPane :

The Swing JOptionPane class provides different methods to prompt the user for different purposes. Commonly used message dialogues are :

  • Showing information message to user.
  • Warn user to display alert messages.
  • Taking input from user.
  • Taking confirmation from user to proceed further.

Let’s implement all the above scenarios :

OptionPaneDemo.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


class OptionPaneDemo extends JFrame
{
    JButton inform,alert,input,confirm;
    JPanel buttonsPanel;
    URL url;

    OptionPaneDemo()
    {
       super("JOptionPane Demo");

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       inform = new JButton("Information");
       inform.addActionListener(new ActionListener()
       {
          public void actionPerformed(ActionEvent ae)
          {
              JOptionPane.showMessageDialog(buttonsPanel,"Hello user this is for your king information.","Just to Inform..",JOptionPane.INFORMATION_MESSAGE);
          }
       });

       alert = new JButton("Alert");
       alert.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent ae)
           {
             
              JOptionPane.showMessageDialog(buttonsPanel,
              "Your Request has been processesed successfully",
              "Alert",JOptionPane.WARNING_MESSAGE);


           }
       });
       
       input = new JButton("Give Input");
       input.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent ae)
           {
              JOptionPane.showInputDialog(buttonsPanel,
              "Can I know your name please ?");
           }
       });
       
       confirm = new JButton("Please Confirm");
       confirm.addActionListener(new ActionListener()
       {
           public void actionPerformed(ActionEvent ae)
           {
              JOptionPane.showConfirmDialog(buttonsPanel,
              "Please confirm us ?");
           }
       });

       buttonsPanel = new JPanel();
       buttonsPanel.add(inform);
       buttonsPanel.add(alert);
       buttonsPanel.add(input);
       buttonsPanel.add(confirm);

       getContentPane().add(buttonsPanel);
       setSize(300,300);
       setVisible(true);
    }
}

public class JOptionPaneDemo {
    public static void main(String args[])
       {
        OptionPaneDemo frame = new OptionPaneDemo();
       }
}

Output :

JOptionPane Example

Information Message :

JOptionPane Example Information

Alert :

JOptionPane Alert

Input :

JOptionPane Input

Confirmation :

JOptionPane Confirm

Happy Learning 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment