• Register
0 votes
107 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);

    }

}

6.9k points

Please log in or register to answer this question.

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;

    }

38.6k points

Related questions

0 votes
1 answer 21 views
21 views
Problem: I am getting this .. can you help me to solve it? No suitable method found for showmessagedialog(string)
asked Apr 26 Ifra 43.4k points
0 votes
1 answer 23 views
23 views
Problem: What exactly is the solution : Exception in thread "main" java.util.illegalformatconversionexception: f != java.lang.string?
asked Jun 12 Mezbah 130k points
0 votes
1 answer 11 views
11 views
Problem: Please assist me in resolving this issue: which method will return the element that has the id attribute with the specified value
asked Apr 1 rakib1 51.5k points
0 votes
1 answer 17 views
17 views
Problem: What if I write return statement in constructor? Is it standard conformant? struct A { A() { return; } }; The above code compiles fine, without any error at ideone. But the following code doesn't: struct A { A() { return 100; } }; It gives ... forbids the second one? Is there any explicit statement? Is the return type in the first example void? Is there any implicit return type at all?
asked Apr 24 Humaira ahmed 50.7k points
0 votes
1 answer 14 views
14 views
Problem: I'm trying to write a Swing application in Java that also runs the Google AppEngine Dev-Server and am running into a strange problem with the Swing Eventloop. Why Java Swing application terminates unexpectedly
asked Apr 24 ummeshani 10.8k points
0 votes
1 answer 7 views
7 views
Problem: Has anyone had this complication before : Programs that operate in a gui environment must be:?
asked May 20 Sawyer Parviz 168k points
1 vote
1 answer 90 views
90 views
Problem: Is here any body willing to teach me that how to do this? No suitable constructor found for arraylist(list<int[]>).
asked Apr 27 PkGuy 25.9k points
0 votes
1 answer 45 views
45 views
Problem: Exception in thread "main" java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it. at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159) at java.awt.Window.<init>(Window.java:432) at ... .java:31) at ShapeCreator.main(ShapeCreator.java:138) What does this error message mean? And how can I solve it?
asked Apr 25 sumaiya simi 43.9k points
0 votes
1 answer 18 views
18 views
Problem Why does the following code have a correct output? int GGT has no return statement, but the code does work anyway?
asked Apr 12 neeraj 10.5k points
0 votes
1 answer 5 views
5 views
Problem: Difference between awt and swing in java?
asked Jul 19 Nahil 128k points