private boolean checkForSave() { // build warning message String message; if (file == null) { message = "File has been modified. Save changes?"; } else { message = "File \"" + file.getName() + "\" has been modified. Save changes?"; } // show confirm dialog int r = JOptionPane.showConfirmDialog( this, new JLabel(message), "Warning!", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); if (r == JOptionPane.YES_OPTION) { // Save File if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { // write the file physWriteTextFile(fileChooser.getSelectedFile(), textView.getText()); } else { // user cancelled save after all return false; } } return r != JOptionPane.CANCEL_OPTION; }
public void exit() { // user is attempting to exit the interpreter. // make sure this is what they want to do // and check if changes need to be saved. int r = JOptionPane.showConfirmDialog( this, new JLabel("Exit Interpreter?"), "Confirm Exit", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (r == JOptionPane.YES_OPTION) { // user still wants to go ahead. // if file not saved then prompt to check // whether it should be. if (!dirty || checkForSave()) { System.exit(0); } } }
private void showErrorDialog(String msg) { // show a dialog window containing the error message JOptionPane.showMessageDialog(this, new JLabel(msg), "Error!", JOptionPane.ERROR_MESSAGE); }