JOptionPane showConfirmDialog

Show a message and get a conformation (like Ok/Cancel).The showConfirmDialog return integers to indicate which button the user choose.

For the confirmation dialog, the return value can be one of the following :

OK_OPTION
CANCEL_OPTION
YES_OPTION
NO_OPTION
CLOSED_OPTION

Program

 

 

 

Program Source

import javax.swing.JOptionPane;

public class Javaapp {
    
    public static void main(String[] args) {
        
        int returnValue=JOptionPane.showConfirmDialog(null,
                                                      "Message",
                                                      "Title",
                                                      JOptionPane.YES_NO_CANCEL_OPTION);
         if(returnValue == 0)
            JOptionPane.showMessageDialog(null,"Button Yes pressed");
         else if(returnValue == 1)
            JOptionPane.showMessageDialog(null,"Button No pressed");
         else if(returnValue == 2)
            JOptionPane.showMessageDialog(null,"Button Cancel pressed");
         else if(returnValue == -1)
            JOptionPane.showMessageDialog(null,"ConfirmDialog Closed");
    }     
}

Leave a Comment