JavaFX Spinner工作不正确。 Spinner.valueProperty()。addListener已更改两次

时间:2017-04-26 09:33:53

标签: java javafx javafx-8 spinner

我不明白为什么,如果 Spinner调用ChangeListener两次。我该怎么做才能打电话一次?

Controller.java

public class Controller {
@FXML
Spinner<Double> spinner;
@FXML
public void initialize(){
    spinner.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(0,350,60,1));

    spinner.valueProperty().addListener(new ChangeListener<Double>() {
        @Override
        public void changed(ObservableValue<? extends Double> observable, Double oldValue, Double newValue) {
            System.out.println("Old Value" + oldValue + "New Value" + newValue);
        }
    });
}}

Main.java

 public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root =     FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }}

输出:

Old Value 60.0 New Value 780.0
Old Value 780.0 New Value 350.0

1 个答案:

答案 0 :(得分:1)

这是因为SpinnerValueFactory

spinner.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(0,350,60,1));

Spinner的最大值为350.您输入780:该值正在更改为此值,因为它将更改回350的最大值。

更新:

默认情况下,当按下 Enter 键时,将提交Spinner编辑器内的值。

为避免自动绑定,您可以尝试覆盖默认提交逻辑:

spinner.getEditor().setOnAction(e -> {

    String text = spinner.getEditor().getText();
    SpinnerValueFactory<Double> valueFactory = spinner.getValueFactory();
    if (valueFactory != null) {
        StringConverter<Double> converter = valueFactory.getConverter();
        if (converter != null) {
            try{
                Double value = converter.fromString(text);
                if (value >= minValue && value <= maxValue)
                    valueFactory.setValue(value);
                else
                    valueFactory.setValue(Math.max(Math.min(maxValue, value), minValue));

                // This ensures that if the value us e.g. 350, then 450 entered, the text 
                // is updated to 350 again (rather than ramaining 450)
                spinner.getEditor().setText(converter.toString(valueFactory.getValue()));
            } catch(NumberFormatException nfe){
                spinner.getEditor().setText(converter.toString(valueFactory.getValue()));
            }
        }
    }

    e.consume();
});

,值定义为:

double maxValue = 350;
double minValue = 0;
spinner.setValueFactory(
    new SpinnerValueFactory.DoubleSpinnerValueFactory(minValue, maxValue, 60, 1));

注意:

最小值和最大值也可以从SpinnerValueFactory中检索 - 遗憾的是不是以完全干净的方式,而是使用一些演员:

spinner.getEditor().setOnAction(e -> {
    SpinnerValueFactory.DoubleSpinnerValueFactory valueFactory = (SpinnerValueFactory.DoubleSpinnerValueFactory) spinner.getValueFactory();

    if (valueFactory != null) {
        StringConverter<Double> converter = valueFactory.getConverter();
        if (converter != null) {
            try {
                valueFactory.setValue(Math.max(Math.min(valueFactory.getMax(), 
                        converter.fromString(spinner.getEditor().getText())), valueFactory.getMin()));

                spinner.getEditor().setText(converter.toString(valueFactory.getValue()));

            } catch(NumberFormatException nfe){
                spinner.getEditor().setText(converter.toString(valueFactory.getValue()));
            }
        }
    }

    e.consume();
});

但在这种情况下,没有必要存储最小值和最大值,它们来自工厂本身:

spinner.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(0, 350, 60, 1));