JAVA – JOptionPane Confirmation Dialog With YES / NO Options

This example shows how to use a JOptionPane confirmation dialog with YES / NO Options.

The response is stored in an integer variable which can be used later in the code to perform an action based on the user’s response.

You can change the button labels be changing the options [] values.

Object[] options = { “Yes”,
                            “No”};

int n =JOptionPane.showOptionDialog(null,
            “\n”
            + “Is today Monday?”
            + “\n\n”,
            “Please Confirm”,
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[1]);

       // YES OPTION CLICKED
       if(n == 0){
           System.out.println(“YES Clicked”);
       }

       // NO OPTION CLICKED
       if(n == 1){
           System.out.println(“NO Clicked”);
       }

       // CROSS CLICKED
       if(n == -1){
           System.out.println(“Cross Clicked”);
       }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s