java - JavaFX action event statement execution order -


public class maincontroller { @fxml public button browse_report; @fxml public button browse_directory; @fxml public button export; @fxml public button close; @fxml public label report; @fxml public label directory; @fxml public label processing; @fxml public textfield report_text; @fxml public textfield directory_text; @fxml public progressbar pg = new progressbar();    public void closebutton(actionevent e) {     system.exit(0); }  public void getreport(actionevent e) {     filechooser fc = new filechooser();     file file=fc.showopendialog(null);     report_text.settext(file.getabsolutepath()); }  public void getdirectory(actionevent e) {     directorychooser dc = new directorychooser();     file file =dc.showdialog(null);     directory_text.settext(file.getabsolutepath());  }  public void export(actionevent e) {      pg.setprogress(-1);     foo();     alert alert = new alert(alerttype.information, "spreadsheet generated", buttontype.close);     alert.showandwait();     pg.setprogress(1); } 

the above export() method, when executed on button click,appears run method statements out of order. progress bar animation not display until after alert window pops up. how can correct this?

assuming foo() method takes long time run, happening blocking fx application thread, prevents performing usual duties, such rendering ui. while statements (of course) executed in order write them, i.e. progressproperty of pg set -1, foo() executed, alert shown, won't see results of in ui until whole process completes (or, actually, in case until relinquish control of fx application thread calling showandwait()).

the basic way solve run foo() in background thread. if there ui-related code want perform when completes, best way use task , use onsucceeded handler perform ui code @ end. in general this:

public void export(actionevent e) {      pg.setprogress(-1);     task<void> task = new task<void>() {         @override         protected void call() throws exception {             foo();             return null ;         }     };     task.setonsucceeded(evt -> {         alert alert = new alert(alerttype.information, "spreadsheet generated", buttontype.close);         alert.showandwait();         pg.setprogress(1);      });     new thread(task).start(); } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -