+ Reply to Thread
Results 1 to 2 of 2

Thread: JOptionPane error

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    14

    JOptionPane error

    I've created a JOptionPane and has only 2buttons YES_NO_OPTION

    When JOptionPane.showConfirmDialog is popped up I want if I clicked 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 what is my mistake.

    My Code:

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

    }
    }

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    13
    You need to look at the return value of the call to showConfirmDialog.

    int dialogResult = JOptionPane.showConfirmDialog (null, "Would You Like to Save your Previous Note First?","Warning",dialogButton);
    if(dialogResult == JOptionPane.YES_OPTION){
    \\ ...
    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.

    Returns: an integer indicating the option selected by the user

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts