java - javafx: bug when trying to close window properly -


i designing close window functionality desktop application. high level explanation of functionality listed:

  1. if click exit menuitem, prompts confirmbox user confirm whether wants save or not before closing application.
  2. if user click on closebutton on window force close window (i.e. setoncloserequest function), exit menuitem event fire off, brings user case (1) again.
  3. within confirmboxcode, have bind enter key save things, n key not save things , escape key close confirmbox.
  4. i have set accelerator exit menuitem (metakey + e).

everything works fine. however, there minor bug if follow special sequence of steps. whenever use accelerator exit menuitem (i.e. metakey + e) and press either 1 of 3 keys(enter, escape, n), confirmbox closes pops again.

i wondering why happening in special case?

public class confirmbox {     // answer[0] determines need save     // answer[1] determines whether close application or not     private static boolean[] answer = new boolean[]{false,false};     private static stage window;      public static boolean[] displaywarning(string title, string message){         window = new stage();         window.initmodality(modality.application_modal);         window.settitle(title);         window.setminwidth(300);         label label = new label();         label.settext(message);         button yesbutton = new button("yes");         button nobutton = new button("no");          // needtosave = true, close application = true , close confirmbox         yesbutton.setonaction(ey ->{             answer[0] = true;             answer[1] = true;             window.close();         });          // needtosave = false, close application = true , close confirmbox         nobutton.setonaction(en -> {             answer[0] = false;             answer[1] = true;             window.close();         });           // needtosave = false, close application = false , close confirmbox         window.setoncloserequest(e -> {             answer[0] = false;             answer[1] = false;             closeconfirmbox();         });          // key binding               window.addeventhandler(keyevent.key_pressed, e -> {             if ( e.getcode() == keycode.n){                 nobutton.fire();                 e.consume();               }         });          // bind enter key yesbutton               window.addeventhandler(keyevent.key_pressed, ev -> {             if (ev.getcode() == keycode.enter ){                   yesbutton.fire();                ev.consume();              }         });           window.addeventfilter(keyevent.key_pressed, ev ->{             if(ev.getcode()==keycode.escape){                 ev.consume();                 answer[0] = false;                 answer[1] = false;                 closeconfirmbox();             }         });          vbox layout = new vbox(20);         layout.setpadding(new insets(20,5,20,5));         hbox bottomlayout = new hbox(50);         bottomlayout.setpadding(new insets(20,5,20,5));         bottomlayout.getchildren().addall(yesbutton,nobutton);         bottomlayout.setalignment(pos.center);         layout.getchildren().addall(label,bottomlayout);         layout.setalignment(pos.center);          scene scene = new scene(layout);         window.setscene(scene);         window.showandwait();         return answer;     }      public static void closeconfirmbox(){         window.close();      } } 

within controller class, how designed menuitem menuitemexit.

        menuitemexit.setonaction(new eventhandler<actionevent>(){             @override              public void handle(actionevent e){                 //system.out.println("set stage" + primarystage);                 boolean[] answer;                 boolean needtosave = false;                 boolean closeapplication = false;                 if(saved.get() == false){                     answer = confirmbox.displaywarning("warning", "do want save stuff?");                     needtosave = answer[0];                     closeapplication = answer[1];                 }                 if(needtosave == true){                     menuitemsave.fire();                 }                 if(closeapplication== true){                     platform.runlater(new runnable() {                         public void run() {                             close();                         }                     });                 }             }         });          primarystage.setoncloserequest(e -> {             e.consume();             menuitemexit.fire();         });          menuitemexit.setaccelerator(new keycodecombination(keycode.e, keycombination.meta_down));      public void close(){         this.primarystage.close();     } 


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -