Java Swing JOptionPane Example

By | 2017-11-12T08:19:08+00:00 November 12th, 2017|java|0 Comments

This tutorial lets you a clear understanding about 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 :

[tie_list type=”checklist”]

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


Lets implement all the above scenarios :

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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;

       super("JOptionPane Demo");


       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)
              "Your Request has been processesed successfully",

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

       buttonsPanel = new JPanel();


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:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment