0

i got this problem i used a JOptionPane in OptionDialog mode but i can't manage to set to default option that if i select the x in the upper right corner it closes itself; this is because in the declaration of the showOptionDialog it makes me selected only one of the Object[] array containing my choices this is the code

Object[] options = {"Vacanza","Cena","Prestazione","Bene"};
int choice = JOptionPane.showOptionDialog(frame,"Nuovo Prodotto","Scegli il prodotto",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);
if (choice == 0) {
       //things to do }
else if (choice == 1) {
        //things to do }...

now i had to set the last parameter of the showoptiondialog as one of my choices resulting then if i select the X in the upper corner it anyway does the default choice when i just want it to close doing nothing, how can i fix this guys? pls help me

The BigBoss
  • 111
  • 2
  • 11

1 Answers1

1

I tried this sample code to test what you are trying. When I click the X it prints out "Something else selected".
Maybe I don't understand your question. Can you clarify?

import javax.swing.*;

public class Helloworld {
    static JFrame frame;

    public Helloworld(){
    }

    public void run(){
        Object[] options = {"Vacanza","Cena","Prestazione","Bene"};
        int choice = JOptionPane.showOptionDialog(
                null,
                "Nuovo Prodotto",
                "Scegli il prodotto",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null,
                options,
                options[0]);
        if(choice == 0){
            System.out.println("0 selected");
        }
        else{
            System.out.println("Something else selected");
        }
    }

    public static void main(String[] args) {
        Helloworld hw = new Helloworld();
        hw.run();
    }
}
Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
  • yeah mate from your code i got what was the problem, because i was selecting choices by using if (choice==0).. if (choice==1) ..if (choice==2).. and then considering the last choice by using only the else, when with the else you even consider the selection on the X in the upper right corner and not only the last choice, ty for your help :) – The BigBoss Jan 29 '14 at 19:35