Actually if you lock at showOptionDialog
you will see that it returns an int
holding the button that got pressed in the dialog, so you can use that int
variable to determine weather to execute you statement or not.
For Example:
int result = JOptionPane.showOptionDialog(null, panel, "Name Image", JOptionPane.CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
if(result == JOptionPane.YES_OPTION) {
//your code.
}