• Register
0 votes
60 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

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;

    }

9 7 4
38,600 points

Related questions

2 votes
1 answer 20 views
20 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, 2020 morrisBson 3.2k points
0 votes
1 answer 33 views
33 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, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 19 views
19 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, 2020 Gavin 15.3k points
0 votes
1 answer 813 views
813 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, 2020 jwilliam 3.9k points
0 votes
1 answer 50 views
50 views
Problem: Hi there! I am a bit curious about what happens if I skip the return type in function declaration. Does the compiler generate error or the program executes normally? If it executes normally, what return type is used as function return type by default?
asked Nov 28, 2020 Code Learner 5.1k points
0 votes
1 answer 6 views
6 views
Can someone please help!! how do I solve this issue?
asked Dec 16, 2020 TeamScript 9.2k points
0 votes
1 answer 94 views
94 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, 2020 samhaz 5k points
0 votes
2 answers 92 views
92 views
I'm new in java programming, I know how to find out the largest or smallest number from an array. But I'm facing problem when I'm trying to find out smallest staring in 3 string. Please help me write a method min that has three string parameters and returns the smallest.
asked May 2, 2020 samhaz 5k points
0 votes
1 answer 12 views
12 views
I want to know how to fix this. Could anyone here help me out?
asked Dec 16, 2020 TeamScript 9.2k points
0 votes
1 answer 79 views