The BigBoss
The BigBoss

Reputation: 111

how to change default choice for JOptionPane.showOptionDialog to default_close_operation

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

Upvotes: 0

Views: 451

Answers (1)

Alvin Bunk
Alvin Bunk

Reputation: 7764

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();
    }
}

Upvotes: 1

Related Questions