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){

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

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.