我有一个TextField,它已经被赋予了TextFormatter,并且该TextFormatter具有一个过滤器,用于接收文本字段的输入,并确保它是双精度格式。然后使用DoubleStringConverter将过滤后的文本转换为双精度值。这是在自定义的FXML组件中完成的。请参见下面的最小示例。
public SomeComponent implements Initializable {
@FXML
public TextField inputField;
public int min;
public void initialize(URL location, ResourceBundle resources) {
UnaryOperator<TextFormatter.Change> doubleFilter = change -> {
String newText = change.getControlNewText();
if (newText.matches("-?[0-9]{1,13}(\\.[0-9]*)?")) {
return change;
}
return null;
};
inputField.setTextFormatter(new TextFormatter<>(new DoubleStringConverter(), min, doubleFilter));
}
}
另一个使用此组件的FXML组件...
public SomeView implements Initializable {
@FXML
SomeComponent comp;
public void initialize(URL location, ResourceBundle resources) {
comp.inputField.getTextFormatter().valueProperty().bindBidirectional(SomeDataManagerClass.SomeSimpleDoubleProperty());
}
}
bindBidirectional()试图绑定TextField的转换值到其他地方已存在的SimpleDoubleProperty,但是会返回以下错误:
method Property.bindBidirectional(Property<CAP#1>) is not applicable
(argument mismatch; SimpleDoubleProperty cannot be converted to Property<CAP#1>)
本质上,我想利用TextField的值(而不是 getText()
获得的值)来绑定到其他属性。
我意识到TextFormatter的捕获类型推断存在问题,但我找不到JavaDocs中任何显示如何使用TextFormatter的double值的地方。如果没有一种简单的方法从TextField中提取该double值,那么使用DoubleStringConverter来转换值有什么意义呢?如果我已经费心设置了一个漂亮的TextFormatter,则使用 Double.parseDouble(inputField.getText())
看起来就毫无意义了。
我如何在SomeComponent之外利用TextFormatter的valueProperty?