java - How to stop an actionListener from executing its code if the user chooses no on a confirm dialog box? -
i have actionlistener set on button. if user clicks it adds 10 progress bar. want allow user cancel button press , prevent code within actionlistener executing. i.e. want program disregard users click if choose no on confirm dialog box.
is try/catch exception way go here?
thanks.
btnfightcrime.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent arg0) { if (joptionpane.showconfirmdialog(null, "this action cost 5 energy points, , you'll gain 5 action points.", "proceed?", joptionpane.yes_no_option) == joptionpane.no_option) { // yes option } else if (joptionpane.yes_no_option==joptionpane.yes_option) { btnfightcrime.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent arg0) { crimes.randomcrime(); fightcrime(iactionbar); random r = new random(); int delay = 0; //delay * 1000 because format timers milliseconds timer = new timer ((delay*0), new actionlistener(){ @override public void actionperformed(actionevent arg0){ btnfightcrime.setvisible(true); btnignore.setvisible(true); lblvillaincrime.setvisible(false); lblherocrime.setvisible(true); frame.validate(); timer.stop(); } }); timer.start(); btnfightcrime.setvisible(false); btnignore.setvisible(false); lblherocrime.setvisible(false); frame.validate(); } }); } } }); if (joptionpane.showconfirmdialog(null, "this action cost 5 energy points, , you'll gain 5 action points.", "proceed?", joptionpane.yes_no_option) == joptionpane.yes_option) { btnfightcrime.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent arg0) { crimes.randomcrime(); fightcrime(iactionbar); random r = new random(); int delay = 0; //delay * 1000 because format timers milliseconds timer = new timer ((delay*0), new actionlistener(){ @override public void actionperformed(actionevent arg0){ btnfightcrime.setvisible(true); btnignore.setvisible(true); lblvillaincrime.setvisible(false); lblherocrime.setvisible(true); frame.validate(); timer.stop(); } }); timer.start(); btnfightcrime.setvisible(false); btnignore.setvisible(false); lblherocrime.setvisible(false); frame.validate(); } }); } else if (joptionpane.yes_no_option==joptionpane.no_option) { }
simply use if
statement , check return state of confirmation dialog...
int result = joptionpane.showconfirmdialog(this, "are sure want destory world?", "destory world", joptionpane.yes_option); if (result == joptionpane.yes_option) { // blow }
of course, use same technique, use flag, if confirmation process separate update process, becomes more difficult
based on updated code
i'm going take stab in dark , "guess" mean...
btnfightcrime.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { if (joptionpane.showconfirmdialog(null, "this action cost 5 energy points, , you'll gain 5 action points.", "proceed?", joptionpane.yes_no_option) == joptionpane.yes_option) { crimes.randomcrime(); fightcrime(iactionbar); random r = new random(); int delay = 0; //delay * 1000 because format timers milliseconds timer = new timer((delay * 0), new actionlistener() { @override public void actionperformed(actionevent arg0) { btnfightcrime.setvisible(true); btnignore.setvisible(true); lblvillaincrime.setvisible(false); lblherocrime.setvisible(true); frame.validate(); timer.stop(); } }); timer.start(); btnfightcrime.setvisible(false); btnignore.setvisible(false); lblherocrime.setvisible(false); frame.validate(); } } };
there no point in doing } else if (joptionpane.yes_no_option==joptionpane.yes_option) {
, (most likely) false
(or @ least, unreliable).
adding actionlistener
inside actionlistener
same button doesn't make sense. ask user, "do wan this?", if answer "yes", do, otherwise, don't, they've clicked button...
Comments
Post a Comment