71

I've created a JOptionPane and it only has two buttons YES_NO_OPTION .

After JOptionPane.showConfirmDialog pops out , I want to click YES BUTTON to continue opening the JFileChooser and if I clicked NO BUTTON it should cancel the operation.

It seems pretty easy but I'm not sure where is my mistake.

Code Snippet:

if (textArea.getLineCount() >= 1) {  //The condition to show the dialog if there is text inside the textArea

    int dialogButton = JOptionPane.YES_NO_OPTION;
    JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous Note First?","Warning",dialogButton);

    if (dialogButton == JOptionPane.YES_OPTION) { //The ISSUE is here

    JFileChooser saveFile = new JFileChooser();
    int saveOption = saveFile.showSaveDialog(frame);
    if(saveOption == JFileChooser.APPROVE_OPTION) {

    try {
        BufferedWriter fileWriter = new BufferedWriter(new FileWriter(saveFile.getSelectedFile().getPath()));
        fileWriter.write(textArea.getText());
        fileWriter.close();
    } catch(Exception ex) {

    }
}
Sobiaholic
  • 2,927
  • 9
  • 37
  • 54

3 Answers3

122

You need to look at the return value of the call to showConfirmDialog. I.E.:

int dialogResult = JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous Note First?","Warning",dialogButton);
if(dialogResult == JOptionPane.YES_OPTION){
  // Saving code here
}

You were testing against dialogButton, which you were using to set the buttons that should be displayed by the dialog, and this variable was never updated - so dialogButton would never have been anything other than JOptionPane.YES_NO_OPTION.

Per the Javadoc for showConfirmDialog:

Returns: an integer indicating the option selected by the user

Edric
  • 24,639
  • 13
  • 81
  • 91
ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • wow it worked! I just started using `showConfirmDialog` and I didn't get it so well, although I read the Javadoc. But now with my mistake and your explanation it cleared a lot of confusion. I will play with this more and see what can I come up with. THANKS!!! – Sobiaholic Dec 31 '11 at 16:24
  • 3
    @iMohammad, why don't you read the Swing tutorial?? The tutorial contains working examples, for all the questions you have been asking over the past few days. – camickr Dec 31 '11 at 16:41
44

Try this,

int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog(this, "Your Message", "Title on Box", dialogButton);
if(dialogResult == 0) {
  System.out.println("Yes option");
} else {
  System.out.println("No Option");
} 
f1sh
  • 11,489
  • 3
  • 25
  • 51
ALV
  • 569
  • 1
  • 5
  • 12
7
int opcion = JOptionPane.showConfirmDialog(null, "Realmente deseas salir?", "Aviso", JOptionPane.YES_NO_OPTION);

if (opcion == 0) { //The ISSUE is here
   System.out.print("si");
} else {
   System.out.print("no");
}
demongolem
  • 9,474
  • 36
  • 90
  • 105
axlfire1
  • 191
  • 2
  • 4
  • 1
    How is this different from [the accepted answer](https://stackoverflow.com/a/8689130/7111561) which was posted long ago which is even better since using the `JOptionPane.YES_OPTION` instead of `0`? – derHugo Dec 13 '18 at 13:36