javafx - Manage wait cursor for task -


  1. i'm outside ui , wish display wait cursor while stuff happening , using basic pattern:

    on ui - primarystage.scene.cursor = cursor.wait try { stuff off ui... } { on ui - primarystage.scene.cursor = cursor.default }

while running can start process completes , cursor restored before first task completes.

i don't mind "waiting" while first task completes, don't think means doing work on ui thread? there built in solution pattern provided in javafx?

  1. my tab contains 2 combo box. when hit 2nd combo box drop down, wait cursor appears on list though cursor default state. if move mouse pointer outside/back on list cursor correctly displayed default. separate issue or somehow related?

view

label 'from' combobox(items: bind(model.wcombofromitemsproperty()), value: bind(model.wcombofromproperty()), selectfromaction) label 'to' combobox(items: bind(model.wcombofromitemsproperty()), value: bind(model.wcombotoproperty()), selecttoaction) 

model

@fxobservable listelement wcombofrom = new listelement() @fxobservable listelement wcomboto = new listelement() @fxobservable list wcombofromitems = fxcollections.observablearraylist() @fxobservable list wcombotoitems = fxcollections.observablearraylist()  final objectproperty<cursor> cursor_default = new simpleobjectproperty<>(cursor.default) final objectproperty<cursor> cursor_wait = new simpleobjectproperty<>(cursor.wait) 

controller

//lifecycle void onreadystart(griffonapplication application) {     loadwindowdata() }  // both combo boxes contain same items protected void loadwindowdata() {     def list = [new listelement(textvalue: '')]     list.addall dataservice.getdata().collect {         new listelement(textvalue: it.name, objectvalue: it)     }      runinsideuiasync {         model.wcombofromitems.addall(list)         model.wcombotoitems.addall(list)     } }  void selectfrom() {     performaction {         gclistfrom = getcontrollist(model.wcombofrom.objectvalue)         settreeitems(model.wtreegcfrom, gclistfrom, model.wcombofrom)         settreeitems(model.wtreegcto, gclistto, model.wcomboto)     } }  void selectto() {     performaction {         gclistto = getcontrollist(model.wcomboto.objectvalue)         settreeitems(model.wtreegcto, gclistto, model.wcomboto)     } }  def performaction = {c ->     task<void> t = new task() {         @override protected void call() {             println "running closure " + isuithread()             c.call()         }     }      runinsideuisync {         application.primarystage.scene.cursorproperty().bind(bindings.when(t.runningproperty())             .then(model.cursor_wait).otherwise(model.cursor_default))     }      runoutsideui(t) } 

other

@equalsandhashcode(includes = 'textvalue') class listelement implements serializable {     string textvalue = ""     serializable objectvalue // serializable object business model      @override     string tostring() {         textvalue     } } 

the griffon framework automatically invokes onaction controller events outside ui thread. groovyfx contains magic adds "onselect" action bound selectionmodel.selecteditemproperty i.e.

class groovyfxenhancer {     static void enhanceclasses() {         ...         combobox.metaclass {             cellfactory << { closure closure -> delegate.setcellfactory(closure callback)}             onselect << { closure closure ->                 delegate.selectionmodel.selecteditemproperty().addlistener(closure changelistener);         }         ...     } } 

is there built in solution pattern provided in javafx?

i advice use built in task ;)

it has predefined methods handle need.

private task<void> backgroundtask = new task() {     @override     protected void call() throws exception {         // on background thread ;         return null;     } }; 

it has runningproperty(), can bind cursorproperty() of scene.

you can create 2 objectproperty<cursor> containing cursor.default , cursor.wait.

final objectproperty<cursor> cursor_default = new simpleobjectproperty<>(cursor.default); final objectproperty<cursor> cursor_wait = new simpleobjectproperty<>(cursor.wait); 

then can bind them task :

scene.cursorproperty().bind(bindings.when(backgroundtask.runningproperty())                                             .then(cursor_wait).otherwise(cursor_default)); 

would separate issue or somehow related?

if action on combobox somehow invoking background thread, might related, else difficult comment.


Comments