Question

My question is the inverse of this one: Is there a way to only have the OK button in a JOptionPane showInputDialog (and no CANCEL button)?

One solution to that was (if I read correctly) to add an arbitrary JPanel, in that instance a label. My problem is that I need a JComboBox object in the message window, and (in the same way that solved Coffee_Table's problem) having the JComboBox seemingly removes the cancel button. It doesn't matter if I replace YES_NO_CANCEL_OPTION with OK_CANCEL_OPTION or QUESTION_MESSAGE.

I'm still at the mindless-copying stage of learning about the JOptionPane family, so I presume the solution is obvious and I just don't know it because I haven't seen any specific examples to mindlessly copy. (Which also means that once I learn how to add a cancel button, I'll need to work on how to access whether the user hit it. EDIT: And I'm half-sure how I'd do it, so you don't need to answer it if you don't want to.)

public static void main(String[] args) {
    int numCh1 = 1;
    String[] moves = {"rock","paper","scissors"};
    JComboBox<?> optionList = new JComboBox<Object>(moves);
    JOptionPane.showMessageDialog(
        null,
        optionList,
        "Player One: Choose a Move",
        JOptionPane.YES_NO_CANCEL_OPTION
    );
    numCh1 = optionList.getSelectedIndex();
    System.out.println(moves[numCh1]);
}

Note: The combo box is non-negotiable (as opposed to, say, three buttons) because my actual project is to simulate rps101; I just figured you didn't need to see all 100 moves (or anything else irrelevant to this question).

Was it helpful?

Solution

You're using the showMessageDialog() method, which shows just that: a message. It doesn't have a cancel option. For that, use one of the other methods.

In fact, that last parameter isn't even valid. It's not looking for an option type like you provided, it's looking for a message type (ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE).

As always, the API is your best friend: http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top