Binding JavaFx:ObjectProperty的绑定<;整数>;使用ObjectProperty<;单位>;

Binding JavaFx:ObjectProperty的绑定<;整数>;使用ObjectProperty<;单位>;,binding,combobox,javafx-8,Binding,Combobox,Javafx 8,我有一个带有NumberSpinner元素和ComboBox元素的场景,我想将NumberSpinner元素的minValue属性与ComboBox元素的valueProperty绑定。一些代码: @FXML private NumberSpinner aNumberSpinner; @FXML private ComboBox<Unit> aComboBox; 我想要的是,当我在aComboBox中选择degreeUnit时,amberspinner的minValueProper

我有一个带有
NumberSpinner
元素和
ComboBox
元素的场景,我想将
NumberSpinner
元素的
minValue
属性与
ComboBox
元素的valueProperty绑定。一些代码:

@FXML
private NumberSpinner aNumberSpinner;
@FXML
private ComboBox<Unit> aComboBox;

我想要的是,当我在
aComboBox
中选择
degree
Unit
时,
amberspinner
minValueProperty()。我怎样才能做到这一点?

正如克利奥帕特拉在评论中所建议的那样,最好是装置知道自己的最小值

首选解决方案-无绑定

我的首选解决方案根本不使用绑定

组合框值的侦听器可以通过查询组合框中新选择的单位的最小值,轻松地将微调器对象的最小值直接设置为适当的值

有时绑定可能有点太复杂了

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() {
            return 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);
    }
}
可执行示例

将其与单位转换结合起来,可以得到以下结果,甚至可以满足您的要求:

ObjectProperty<Integer> op = new SimpleObjectProperty<>(5);
op.bind(
    IntegerExpression.integerExpression(
        Bindings.when(
            combo.valueProperty().isEqualTo(Unit.degree)
        ).then(10)
         .otherwise(0)
    ).asObject()
);
ObjectProperty op=newsimpleobjectproperty(5);
op.bind(
IntegerExpression.IntegerExpression(
绑定。什么时候(
combo.valueProperty().isEqualTo(单位度)
).然后(10)
。否则(0)
).asObject()
);

咳嗽。。单元本身应该决定它自己的最小值(与不相关的绑定相比),谢谢!aNumberSpinner.minValueProperty()是一个ObjectProperty,因此我有一个编译错误:方法bind(ObservaleValueNumberBinding(或IntegerBinding,忘记确切位置)有一个工厂方法将属性转换为TYPEPropertySorry,但我想我必须从IntegerProperty获取属性。我知道有asObject()方法,但现在我的问题是如何“转换”IntegerProperty中的一个数字。a)让单元决定它自己的最小值b)将滑块的最小值绑定到组合的selectedItem,也就是说,选定的unitI可以有更多的组合框与更多的NumberSpinner连接,并且每个NumberSpinner可以有不同的minValue。如果我让单位决定它自己的最小值,我就无法做到这一点。对我来说,这听起来像是一个可疑的设计-视图如何能够决定哪些“不同”值可能适用?我的视图有更多的数字指针。例如,我可以使用jewelsea的纯绑定解决方案为numberSpinner1设置minValue=10,为numberSpinner2设置minValue=20。现在你完全把我搞糊涂了(当然是我的错,不是你的:-),那么绑定到单元组合是怎么回事?是不是也有不同的组合(每个滑块一个)?
Bindings.when(
        combo.valueProperty().isEqualTo(Unit.degree)
    ).then(10)
     .otherwise(0)
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, degree }

    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);
    }
}
ObjectProperty<Integer> op = new SimpleObjectProperty<>(5);
op.bind(
    IntegerExpression.integerExpression(
        slider.minProperty()
    ).asObject()
);
ObjectProperty<Integer> op = new SimpleObjectProperty<>(5);
op.bind(
    IntegerExpression.integerExpression(
        Bindings.when(
            combo.valueProperty().isEqualTo(Unit.degree)
        ).then(10)
         .otherwise(0)
    ).asObject()
);