i want to disable the NO and CANCEL option , can you guys help me because im only a begineer of JOPTION here is ma code: btw this is my assignment in my 1st year college "compro" my teacher tell us to create a program , and my system is QUESTION and ANSWER my problem is there was a NO and CANEL option what if the teacher click it then the null will appear , (all i need is, if the teacher click the NO or CANCEL the program will exit .)

import javax.swing.*;
import java.io.*;
public class QA
{
    public static void main(String[]args)throws IOException{

    DataInputStream m = new DataInputStream(System.in);

        JFrame frame = new JFrame("Message");

    JOptionPane.showMessageDialog(frame,
        "WELCOME TO THE WORLD OF JAVA");

    String name= JOptionPane.showInputDialog
    ("Enter your Full Name");

    JOptionPane.showConfirmDialog(null, 
    "Are you Ready"+name, "Choose answer", JOptionPane.YES_NO_OPTION);

        Object[] possibleValues = { "Select answer below","__________",
        "computer", "hardware", "software", "jboy", "rommel", "warren"};
        Object selectedValue = JOptionPane.showInputDialog(null,    
        "It is an electronical device capable of performing complex computations is a short time?", 
        "QUESTION 1",JOptionPane.INFORMATION_MESSAGE, null,possibleValues, possibleValues[0]);

        String choice = selectedValue.toString();
        String answer = "computer";

        if (choice.equals(answer)){

            Object[] options = { "OK", "CANCEL" };
            JOptionPane.showOptionDialog(null, "Your Answer is Correct", "Congrats", 
            JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
            null, options, options[0]);

        }else{

            JOptionPane.showMessageDialog(null, "Your answer is wrong", "Owh-owh", JOptionPane.ERROR_MESSAGE); 

        }

        JFrame frames = new JFrame("Message");

        // show a joptionpane dialog using showMessageDialog
         JOptionPane.showMessageDialog(frame,
        "Click OK to proceed for next question");

        Object[] possibleValues1 = { "Select answer below","__________",
        "rommel", "warren", "jboy" };
        Object selectedValue1 = JOptionPane.showInputDialog(null,   
        "WHO'S LOOK A LIKE OF MR.BEAN?", 
        "QUESTION 2",JOptionPane.INFORMATION_MESSAGE, null,possibleValues, possibleValues[0]);

        String choices = selectedValue.toString();
        String answers = "jboy";

        if (choice.equals(answer)){

            Object[] options = { "OK", "CANCEL" };
            JOptionPane.showOptionDialog(null, "Your Answer is Correct", "Congrats", 
            JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE,
            null, options, options[0]);

        }else{

            JOptionPane.showMessageDialog(null, "Your answer is wrong", "Owh-owh", JOptionPane.ERROR_MESSAGE); 

        }



         System.exit(0);











    }
}

Dani AI

Generated

you do not need to disable buttons. Just decide what to do when the user clicks them. JOptionPane returns a value you can check. If the user clicks NO or closes the dialog, exit. If they click CANCEL on an input dialog, you get null, so check for that before calling toString() (otherwise you will get a NullPointerException). is right to point you to the docs, but here is a drop-in pattern you can use now.

// 1) Yes/No question
int ready = JOptionPane.showConfirmDialog(
        null,
        "Are you ready, " + name + "?",
        "Choose answer",
        JOptionPane.YES_NO_OPTION);

if (ready != JOptionPane.YES_OPTION) {
    System.exit(0); // exits on NO or the window close (CLOSED_OPTION)
}

// 2) Multiple-choice input
Object[] choices = { "Select answer below", "__________", "computer", "hardware", "software" };
Object pick = JOptionPane.showInputDialog(
        null,
        "It is an electronic device capable of complex computations?",
        "QUESTION 1",
        JOptionPane.QUESTION_MESSAGE,
        null,
        choices,
        choices[0]);

if (pick == null) {
    System.exit(0); // CANCEL or window close
}

if ("computer".equalsIgnoreCase(String.valueOf(pick))) {
    JOptionPane.showOptionDialog(
            null,
            "Your answer is correct",
            "Congrats",
            JOptionPane.DEFAULT_OPTION,
            JOptionPane.INFORMATION_MESSAGE,
            null,
            new Object[] { "OK" },  // only an OK button
            "OK");
} else {
    JOptionPane.showMessageDialog(null, "Your answer is wrong", "Oops", JOptionPane.ERROR_MESSAGE);
}

A few fixes based on your code: use the array that matches each question (you passed possibleValues for question 2 by mistake), always null-check results from showInputDialog, and prefer "expected".equalsIgnoreCase(actual) to avoid NPEs. Finally, you cannot add a CANCEL button to YES_NO_OPTION; if you see CANCEL, it came from an input dialog returning null.

Do some reading on JOptionPane. The answers are in that article. Next time post your code in code blocks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.