I created JOptionPane and it has only two YES_NO_OPTION buttons.
After JOptionPane.showConfirmDialog pops up, I want to press YES BUTTON to continue opening JFileChooser , and if I pressed NO BUTTON , it should cancel the operation.
It looks pretty easy, but I'm not sure where my mistake is.
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){ } }
java swing jfilechooser
Sobiaholic Dec 31 '11 at 16:14 2011-12-31 16:14
source share