combobox - JavaFx: binding of a ObjectProperty<Integer> with a ObjectProperty<Unit> -
i have scene numberspinner element , combobox element , want bind minvalue property of numberspinner element valueproperty of combobox element. code:
@fxml private numberspinner anumberspinner; @fxml private combobox<unit> acombobox; where unit enum:
public enum unit { mm, grade } what want when take degree unit in acombobox minvalueproperty() of anumberspinner become 10. how can accomplish it?
as suggested kleopatra in comments best if unit knows own minimum.
preferred solution - no binding
my preferred solution wouldn't utilize binding @ all.
a listener on combobox value can set minimum value of spinner object straight appropriate value querying minimum value unit newly selected in combo box.
sometimes possible bit tricky bindings...
import javafx.application.application; import javafx.collections.fxcollections; import javafx.geometry.insets; import javafx.scene.scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.stage; public class unitminimums extends application { private enum unit { mm(0), degree(10); private final int minvalue; private unit(int minvalue) { this.minvalue = minvalue; } public int getminvalue() { homecoming minvalue; } } private slider slider = new slider(0, 20, 0); private combobox<unit> combo = new combobox<>( fxcollections.observablearraylist( unit.values() ) ); @override public void start(stage stage) throws exception { combo.valueproperty().addlistener((observable, oldvalue, newvalue) -> slider.setmin(newvalue.getminvalue()) ); slider.setshowtickmarks(true); slider.setshowticklabels(true); vbox layout = new vbox(5, slider, combo); layout.setpadding(new insets(10)); vbox.setvgrow(combo, priority.always); combo.setmaxwidth(double.max_value); combo.getselectionmodel().select(0); stage.setscene(new scene(layout)); stage.show(); } public static void main(string[] args) { launch(args); } } pure binding solution
if did want pure binding solution, below, has disadvantage of scattering info specific minimum value of unit (which intrinsic enum) around code if started writing code lot.
use bindings.when:
bindings.when( combo.valueproperty().isequalto(unit.degree) ).then(10) .otherwise(0) executable sample
import javafx.application.application; import javafx.beans.binding.bindings; import javafx.collections.fxcollections; import javafx.geometry.insets; import javafx.scene.scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.stage; public class boundminimums extends application { private enum unit { mm, grade } private slider slider = new slider(0, 20, 0); private combobox<unit> combo = new combobox<>( fxcollections.observablearraylist( unit.values() ) ); @override public void start(stage stage) throws exception { slider.minproperty().bind( bindings.when( combo.valueproperty().isequalto(unit.degree) ).then(10) .otherwise(0) ); slider.setshowtickmarks(true); slider.setshowticklabels(true); vbox layout = new vbox(5, slider, combo); layout.setpadding(new insets(10)); vbox.setvgrow(combo, priority.always); combo.setmaxwidth(double.max_value); stage.setscene(new scene(layout)); stage.show(); } public static void main(string[] args) { launch(args); } } on datatype conversion
this gets little complicated , non-obvious me (which reason prefer listeners , straight setters on binding), think can below, coverts doubleproperty slider.minproperty() objectproperty<integer>:
objectproperty<integer> op = new simpleobjectproperty<>(5); op.bind( integerexpression.integerexpression( slider.minproperty() ).asobject() ); putting unit conversion, following, maybe want:
objectproperty<integer> op = new simpleobjectproperty<>(5); op.bind( integerexpression.integerexpression( bindings.when( combo.valueproperty().isequalto(unit.degree) ).then(10) .otherwise(0) ).asobject() ); binding combobox javafx-8
No comments:
Post a Comment