i need add simplemapproperty javafx service class, not sure of correct syntax of if using correct approach. note not trying make javafx service appear java bean, need know how listen updates enummap enum moduletype (that can typea or typeb) , associated boolean flag. essentially, can thought of pair of watchdog timers wrapped in single enummap.
i having trouble understanding how add underlying enummap entries (there should 2 - 1 each moduletype described above).
public class udplistenerservice extends service<void> { // 'watchdog' property private final mapproperty<moduletype, boolean> watchdog; // 'watchdog' simplemapproperty bound property getter public observablemap<moduletype, boolean> getwatchdog() { return watchdog.get(); } // 'watchdog' simplemapproperty bound property setter public void setwatchdog(observablemap<moduletype, boolean> avalue) { watchdog.set(avalue); } // 'watchdog' simplemapproperty bound property public mapproperty<moduletype, boolean> watchdogproperty() { return watchdog; } /** * constructor */ public udplistenerservice() { this.watchdog = new simplemapproperty<>( fxcollections.observablehashmap()); } @override protected task<void> createtask() { return new task<void>() { @override protected void call() throws exception { updatemessage("running..."); while (!iscancelled()) { try { thread.sleep(1000); platform.runlater(() -> { try { // update processing here // . . . // pet watchdog // setwatchdog if (testformoduletype==moduletype.typea) { // please syntax setwatchdog(moduletype.typea, false); } else { // please syntax setwatchdog(moduletype.typeb, false); } } catch (statusruntimeexception ex) { // watchdog timed out - listener // update gui components if (testformoduletype==moduletype.typea) { // please syntax setwatchdog(moduletype.typea, true); } else { // please syntax setwatchdog(moduletype.typeb, true); } } }); } catch (interruptedexception ex) {} } updatemessage("cancelled"); return null; } }; } }
the way use class in javafx controller class add listener populates java gui elements depending on whether associated boolean flag true or false.
usually readonly map property used kind of behavior, i.e. observablemap
field getter. contents of map modified; no new map assigned field after initial map assigned.
private final observablemap<moduletype, boolean> watchdog; public observablemap<moduletype, boolean> getwatchdog() { return watchdog; }
the map modified same way java.util.map
modified, e.g. in case using put
method. changes can observed e.g. using mapchangelistener
or bindings.valueat
.
furthermore enummap
can used backing map
observablemap
, observablemap
method needs used instead of observablehashmap
method.
the following example randomly selects / deselects values of 2 checkboxes based on values in observablemap
.
private checkbox checkboxa; private checkbox checkboxb; private observablemap<moduletype, boolean> map; @override public void start(stage primarystage) { checkboxa = new checkbox("type a"); checkboxb = new checkbox("type b"); map = fxcollections.observablemap(new enummap<>(moduletype.class)); initmaplisteners(); thread t = new thread(() -> { random random = new random(); while (true) { try { thread.sleep(1000); } catch (interruptedexception ex) { } boolean b1 = random.nextboolean(); boolean b2 = random.nextboolean(); platform.runlater(() -> { map.put(moduletype.typea, b1); map.put(moduletype.typeb, b2); }); } }); t.setdaemon(true); t.start(); scene scene = new scene(new vbox(10, checkboxa, checkboxb)); primarystage.setscene(scene); primarystage.show(); }
both following implementations of initmaplisteners()
both set checkbox.selected
states based on map values.
private void initmaplisteners() { checkboxa.selectedproperty().bind(bindings.valueat(map, moduletype.typea)); checkboxb.selectedproperty().bind(bindings.valueat(map, moduletype.typeb)); }
private void initmaplisteners() { map.addlistener((mapchangelistener.change<? extends moduletype, ? extends boolean> change) -> { if (change.wasadded()) { if (change.getkey() == moduletype.typea) { checkboxa.setselected(change.getvalueadded()); } else if (change.getkey() == moduletype.typeb) { checkboxb.setselected(change.getvalueadded()); } } }); }
Comments
Post a Comment