I have a program that when an edit button is pressed the window goes into “editing mode” where you can change fields as you please and then when you press a save button it saves what you have done. The way I want it, if you are in editing mode, and then press another button I want a dialog box to come up to ask if you want to save (yes/no).
Right now I have it prompting for a save if it is in “editing” mode, and if the user presses yes, it will save the file, if anything else is pressed it will do what ever the button is supposed to do.
This is what I have currently. I want to know if there is an easier way to do this instead of putting the actions of the button in there twice.
if (editing) {
save = JOptionPane.showConfirmDialog(this, "Would you like to save?", "Save",
JOptionPane.YES_NO_OPTION);
if (save == 0) {
saveFile();
} else {
//Button actions here...
}
} else {
//Button actions here...
}
It’s correct way, but you should rather compare the result of
showConfirmDialog()withJOptionPaneconstant :Not only it’s better readable by others but also your code will still work if the constants are changed one day (however it is very unlikely).