Programmers learn & share
0 votes
27 views

Problem :

I want to create a message with a Yes or No button. After that a window will appear with a certain message which will depend on if the user clicked Yes or No.

Following is my code:

public class TestJoptionPane {

    public static void main(String[] args){

      //default icon and custom title

        int dialoge = JOptionPane.showConfirmDialog(

            null,

            "Would you like green eggs and ham?",

            "An Inane Question",

            JOptionPane.YES_NO_OPTION);

         if(true){

            JOptionPane.showMessageDialog(null, "HELLO");

        }

        else {

            JOptionPane.showMessageDialog(null, "GOODBYE");

        }

System.exit(0);

    }

}

by (6.9k points)   | 27 views

1 Answer

0 votes

Solution :

For better understanding please follow below code snippets.

int dialoge = JOptionPane.showConfirmDialog(null, "Yes No Cancel", "YesNoCancel", JOptionPane.YES_NO_CANCEL_OPTION);

    if(dialoge == 0)

        {

        JOptionPane.showConfirmDialog(null, "You pressed YES\n"+"Your pressed value is = "+ dialoge);

        }

    else if(dialoge == 1)

        {

        JOptionPane.showConfirmDialog(null, "You pressed NO\n"+" Your pressed value is = "+ dialoge);

        }

    else if (dialoge == 2)

        {

        JOptionPane.showConfirmDialog(null, "You pressed CANCEL\n"+" Your pressed value is = "+ dialoge);

        }

    else if (dialoge == -1)

        {

        JOptionPane.showConfirmDialog(null, "You pressed X\n"+" Your pressed value is = "+ dialoge);

        }

OR

int dialoge = JOptionPane.showConfirmDialog(null, "Yes No Cancel", "YesNoCancel", JOptionPane.YES_NO_CANCEL_OPTION);

    switch (dialoge) {

        case 0:

            JOptionPane.showConfirmDialog(null, "You pressed YES\n"+" Your pressed value is = "+ dialoge);

            break;

        case 1:

            JOptionPane.showConfirmDialog(null, "You pressed NO\n"+" Your pressed value is = "+ dialoge);

            break;

        case 2:

            JOptionPane.showConfirmDialog(null, "You pressed CANCEL\n"+" Your pressed value is = "+ dialoge);

            break;

        case -1:

            JOptionPane.showConfirmDialog(null, "You pressed X\n"+" Your pressed value is = "+ dialoge);

            break;

        default:

            break;

    }

by (36.1k points)  
2,245 questions
2,807 answers
60 comments
241 users