Hi guys,

Im trying some stuff out in java but im a little stuck and confused by stackoverflow answers so I wanted to ask here if its ok.

I have a lot of code but I just took out this one little part that i wanna be able for a user to click and choose the answer he/she wants to see in an JOptionPane window with a text stating : "Click on the button "Right" if you want to see the right and answer, or click on button "wrong" if you want to see the wrong answer.

And then 2 buttons in this pane of course, one called right and one wrong. How do i do that? So when user clicks the button named right she/he will be able to see the right answers and vice versa. Is that possible with JOptionPane? Is it supposed to work with confirmDialog? Thanks in advance. I love this forum.

public void printValue(){ //Method to print out the wrong and right values 

System.out.println("Wrong values are");
    for(Double d:NotOkweight )

    System.out.println("Right values are");
    for(Double d:Okweight){

Edited by pritaeas: Fixed markdown.

2 Years
Discussion Span
Last Post by GoneGirl

you say that this question is about a JOptionPane method, yet you don't tell us which one you are talking about.
I'll just assume you are talking about a ConfirmationDialog. well, the showConfirmationDialog returns an int, based on which button you pressed. so, yes, you can tell which button was pressed, and have your code react to it.

just follow the next snippet:

    int a = JOptionPane.showConfirmDialog(null, "enter");
    if ( a == JOptionPane.OK_OPTION){
        System.out.println("ok pressed");
   if ( a == JOptionPane.YES_OPTION){
        System.out.println("yes pressed");
    if ( a == JOptionPane.NO_OPTION){
        System.out.println("no pressed");
    if ( a == JOptionPane.CANCEL_OPTION){
        System.out.println("cancel pressed");

Oh im sorry, i guess i wasnt clear enough but you understood me right, and that was exaclty the kind of thing i was looking for. Thanks a million


Just FYI - you can also chose what text to show on the buttons, which will make things clearer for the user, eg
Object[] options = { "Right Answer", "Wrong Answer" };
.. which you use with the showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) method.

See the API doc for details


yes thank you, that is what i want but i keep getting errors... well ill check out the api. thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.