Java JOptionPane

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java JOptionPane

The JOptionPane class is used to provide standard dialog boxes such as message dialog box, confirm dialog box and input dialog box. These dialog boxes are used to display information or get input from the user. The JOptionPane class inherits JComponent class.

JOptionPane class declaration

  1. public class JOptionPane extends JComponent implements Accessible  

Common Constructors of JOptionPane class

Constructor

Description

JOptionPane()

It is used to create a JOptionPane with a test message.

JOptionPane(Object message)

It is used to create an instance of JOptionPane to display a message.

JOptionPane(Object message, int messageType

It is used to create an instance of JOptionPane to display a message with specified message type and default options.

 

Common Methods of JOptionPane class

Methods

Description

JDialog createDialog(String title)

It is used to create and return a new parentless JDialog with the specified title.

static void showMessageDialog(Component parentComponent, Object message)

It is used to create an information-message dialog titled "Message".

static void showMessageDialog(Component parentComponent, Object message, String title, int messageType)

It is used to create a message dialog with given title and messageType.

static int showConfirmDialog(Component parentComponent, Object message)

It is used to create a dialog with the options Yes, No and Cancel; with the title, Select an Option.

static String showInputDialog(Component parentComponent, Object message)

It is used to show a question-message dialog requesting input from the user parented to parentComponent.

void setInputValue(Object newValue)

It is used to set the input value that was selected or input by the user.

Java JOptionPane Example: showMessageDialog()

  1. import javax.swing.*;  
  2. public class OptionPaneExample {  
  3. JFrame f;  
  4. OptionPaneExample(){  
  5.     f=new JFrame();  
  6.     JOptionPane.showMessageDialog(f,"Hello, Welcome to Javatpoint.");  
  7. }  
  8. public static void main(String[] args) {  
  9.     new OptionPaneExample();  
  10. }  
  11. }  

Java JOptionPane Example: showMessageDialog()

  1. import javax.swing.*;  
  2. public class OptionPaneExample {  
  3. JFrame f;  
  4. OptionPaneExample(){  
  5.     f=new JFrame();  
  6.     JOptionPane.showMessageDialog(f,"Successfully Updated.","Alert",JOptionPane.WARNING_MESSAGE);     
  7. }  
  8. public static void main(String[] args) {  
  9.     new OptionPaneExample();  
  10. }  
  11. }  

Output:

Java Joptionpane 2

Java JOptionPane Example: showInputDialog()

  1. import javax.swing.*;  
  2. public class OptionPaneExample {  
  3. JFrame f;  
  4. OptionPaneExample(){  
  5.     f=new JFrame();   
  6.     String name=JOptionPane.showInputDialog(f,"Enter Name");      
  7. }  
  8. public static void main(String[] args) {  
  9.     new OptionPaneExample();  
  10. }  
  11. }  

Output:

Java Joptionpane 3

Java JOptionPane Example: showConfirmDialog()

  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. public class OptionPaneExample extends WindowAdapter{  
  4. JFrame f;  
  5. OptionPaneExample(){  
  6.     f=new JFrame();   
  7.     f.addWindowListener(this);  
  8.     f.setSize(300300);  
  9.     f.setLayout(null);  
  10.     f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  
  11.     f.setVisible(true);  
  12. }  
  13. public void windowClosing(WindowEvent e) {  
  14.     int a=JOptionPane.showConfirmDialog(f,"Are you sure?");  
  15. if(a==JOptionPane.YES_OPTION){  
  16.     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  17. }  
  18. }  
  19. public static void main(String[] args) {  
  20.     new  OptionPaneExample();  
  21. }     
  22. }  

Output:

Java Joptionpane 4