JOptionPane showOptionDialog Returns

Show a message and get a user option from a set of options.

JOptionPane.showOptionDialog(null                                                       // position the dialog, the default position is center.

, “Message?”                                            // display Message
, “Title”                                                      // Title in titlebar
, JOptionPane.NO_OPTION             // Option type
, JOptionPane.PLAIN_MESSAGE   // messageType
, null                                                          // Icon (none)
, buttons                                                   // Button text as above.
, “default”);                                              // Default button’s label

The optionReturns variable is get the integer value from the showOptonDialog method.

Program

 

 

Program Source

import javax.swing.JOptionPane;

public class Javaapp {
    public static void main(String[] args) {
     
        String buttons[]={"button1","button2","button3","default"};
        int optionReturns = JOptionPane.showOptionDialog(null
                                                        ,"Message"
                                                        ,"Title"
                                                        ,JOptionPane.NO_OPTION
                                                        ,JOptionPane.PLAIN_MESSAGE
                                                        ,null
                                                        ,buttons
                                                        ,"default");
        if(optionReturns == 0)
            JOptionPane.showMessageDialog(null,"button1 Pressed");
        else if(optionReturns == 1)
            JOptionPane.showMessageDialog(null,"button2 Pressed");
        else if(optionReturns == 2)
            JOptionPane.showMessageDialog(null,"button3 Pressed");
        else if(optionReturns == 3)
            JOptionPane.showMessageDialog(null,"Default button Pressed");
        else if(optionReturns == -1)
            JOptionPane.showMessageDialog(null,"Options Closed");
    }  
}

Leave a Comment