Tuesday , November 21 2017
Home / java / Java Swing JOptionPane Example

Java Swing JOptionPane Example

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 :

  • 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 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 chandrashekhar

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+ ,

Recommended

How to add dynamic files to JTree

In the previous tutorial, we learn about a simple JTree. This tutorial is for adding …

Leave a Reply

Your email address will not be published. Required fields are marked *