【发布时间】:2023-03-12 01:55:01
【问题描述】:
在我的应用程序中,我有一个屏幕,其中包含我需要验证的三个字段和一个保存按钮。
我将保存按钮“禁用属性”绑定到匹配某些模式的字段“文本属性”(本例中的简化模式无关紧要):
public void initialize(){
saveNetworkBtn.disableProperty()
.bind(textPropertyBindingPattern(ipInp, Pattern.compile("[0-9]{3}"))
.or(textPropertyBindingPattern(subnetInp, Pattern.compile("[0-9]{3}")))
.or(textPropertyBindingPattern(gatewayInp, Pattern.compile("[0-9]{3}"))));
}
private BooleanBinding textPropertyBindingPattern(TextField textField, Pattern pattern ) {
return Bindings.createBooleanBinding(() ->
!pattern.matcher(textField.getText()).matches(), textField.textProperty());
}
但我还需要用一些红色边框突出显示不正确的字段,例如
-fx-border-color: red;
我的主要目标是在一个地方处理这个问题,而不是添加多个侦听器。 是否可以在上面的代码中以某种方式对此进行处理,或者我需要为每个字段添加侦听器?
【问题讨论】:
-
我建议单独绑定每个属性。所以是的,将字段的边框或样式属性绑定到其文本。如果你有很多这样的字段,子类
TextField并将这个绑定行为直接添加到它。这样您就不必单独绑定每个字段。
标签: java user-interface javafx fxml