是否可以在创建后调整OptionEditor子类的值?
我使用示例http://docs.gluonhq.com/samples/notes/中的SliderEditor代码创建SliderEditor。
public class SliderOption extends OptionBase<Number> {
private final int min;
private final int max;
public SliderOption(Node graphic, String caption, String description, String category, IntegerProperty value,
boolean isEditable, int min, int max) {
super(graphic, caption, description, category, (Property<Number>) value, isEditable);
this.min = min;
this.max = max;
}
@Override
public Property<Number> valueProperty() {
return value;
}
@Override
public Optional<Function<Option<Number>, OptionEditor<Number>>> editorFactoryProperty() {
return Optional.of(option -> new SliderEditor(option, min, max));
}
public class SliderEditor implements OptionEditor<Number> {
private final Slider slider;
public SliderEditor(Option<Number> option, int min, int max) {
slider = new Slider(min, max, option.valueProperty().getValue().doubleValue());
slider.setSnapToTicks(true);
slider.setMajorTickUnit(1);
slider.setMinorTickCount(0);
slider.setShowTickLabels(true);
valueProperty().bindBidirectional(option.valueProperty());
}
@Override
public Node getEditor() {
return slider;
}
@Override
public Number getValue() {
return slider.getValue();
}
@Override
public void setValue(Number value) {
slider.setValue(value.doubleValue());
}
@Override
public final Property<Number> valueProperty() {
return (Property<Number>) slider.valueProperty();
}
}
}
创建settingsPane和滑块:
SettingsPane settingsPane = new SettingsPane();
settingsPane.setSearchBoxVisible(false);
final Option<Number> sliderOption = new SliderOption(MaterialDesignIcon.PERSON.graphic(),
"Text", "Set the text size", null,settings.getProperty() , true, 0, 10);
settingsPane.getOptions().addAll(sliderOption, new DefaultOption(Option.SEPARATOR));
效果很好,但在一个buttonEvent我想禁用滑块:
Slider slider = (Slider)settingsPane.getOptionEditorFactory().call(sliderOption).getEditor();
slider.setDisable(true);
slider.setValue(10);
禁用什么也没做,也试过setmax / setshowticklabels没有工作,除了setvalues它就像一个魅力。好像我不能修改滑块除了它的值。我做错了什么?
答案 0 :(得分:0)
这不起作用,因为setDisable
未在原始Slider
上调用,而是在由工厂方法创建的新实例上调用:
@Override
public Optional<Function<Option<Number>, OptionEditor<Number>>> editorFactoryProperty() {
return Optional.of(option -> new SliderEditor(option, min, max, slider));
}
因此,不是每次都创建一个新实例,而是必须返回原始实例:
public class SliderOption extends OptionBase<Number> {
private final SliderEditor sliderEditor;
public SliderOption(Node graphic, String caption, String description, String category, IntegerProperty value,
boolean isEditable, int min, int max) {
super(graphic, caption, description, category, value, isEditable);
sliderEditor = new SliderEditor(this, min, max);
}
@Override
public Property<Number> valueProperty() {
return value;
}
@Override
public Optional<Function<Option<Number>, OptionEditor<Number>>> editorFactoryProperty() {
// return Optional.of(option -> new SliderEditor(option, min, max, slider));
return Optional.of(option -> sliderEditor);
}
}
禁用什么都不做,也试过setmax / setshowticklabels没有工作,除了setvalues它就像一个魅力。
setValue的工作原理是因为新Slider
实例的valueProperty与现有Option
实例的valueProperty双向绑定,该实例也绑定到原始Slider
。