• Register
0 votes
48 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 5 3
6,930 points

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;

    }

9 7 4
38,600 points

Related questions

2 votes
1 answer 14 views
14 views
Problem: I am beginner to jTable. I have already attached the jTable to JDBC database. But I don’t know how to retrieve them. My requirement is the saved data must be shown if I restart my program but currently it is not working for me. I have ... studentName"); String myshool = myrs.getString("schoolName"); String myjoining = myrs.getString("joiningDate"); } Can somebody solve my problem ?
asked Mar 24 morrisBson 3.2k points
0 votes
1 answer 11 views
11 views
Problem: I am a beginner to java, swing, netbeans and promise. Now I am trying to read and write the Extensible Linking Format (ELF). Please find below the line of code at which I am facing the Error: p = r.exec("./optimizer " + sourceFile + " ... .java:620) at java.lang.Runtime.exec(Runtime.java:450) at java.lang.Runtime.exec(Runtime.java:347) at GUIMode.GUIMode.actionPerformed(GUIMode.java:213)
asked Aug 26 Raphael Pacheco 4.9k points
1 vote
1 answer 17 views
17 views
Problem: I am a beginner level java student. So my question might be very easy for you guys. I am just wondering if it is possible to return multiple values in a single program? If it is possible, can you guys tell me the process, please? -Best regards
asked Mar 9 Gavin 15.3k points
0 votes
1 answer 482 views
482 views
Problem : I want to input some of the details about student to my program. But when I press a save button its giving me the below listed exception error: "Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet."
asked Jan 31 jwilliam 3.9k points
0 votes
1 answer 44 views
44 views
I start programming recently and I love to do programming but every day I'm facing new problems. I wrote a java code where I can easily take two int input from a user and I'm able to print the smallest value. But I'm doing all things in the main method. I don't want to do ... smallest; if(a>b){ smallest = b; }else{ smallest = a; } System.out.println("Smallest is: "+smallest); } Here is my code.
asked May 3 samhaz 5k points