简体   繁体   English

JavaFX单选按钮禁用/重新启用文本字段

[英]JavaFX Radio button disable/reenable text fields

@FXML
private void isDelivery(ActionEvent event){
    if (rdDelivery.isArmed() == true){
        txtAddress.setDisable(true);
        txtEmail.setDisable(true);
    }
    else    {
        txtAddress.setDisable(true);
        txtEmail.setDisable(true);
    }
}

This code works to disable the text fields but after the first press they stay disabled and wont come back on when the radio button is pressed over and over. 这段代码可以禁用文本字段,但是第一次按下后,它们将保持禁用状态,并且一遍又一遍地按下单选按钮时,它们将不会重新打开。 So after first press the textfields become disabled permanently 因此,在第一次按下文本框后,该文本框将被永久禁用

You got a typo in this logic: 您在此逻辑中有错别字:

if (rdDelivery.isArmed() == true){
    txtAddress.setDisable(true);
    txtEmail.setDisable(true);
}

Change to: 改成:

@FXML
private void isDelivery(ActionEvent event){
    txtAddress.setDisable(!rdDelivery.isArmed());
    txtEmail.setDisable(!rdDelivery.isArmed());
}

It's not clear from your post if you want these fields enabled when the radio button is selected, and not otherwise, so I assumed that the fields are not disabled when the radio button is selected. 从您的帖子中还不清楚您是否希望在选择单选按钮时启用这些字段,否则是否要启用这些字段,因此我假设在选择单选按钮时禁用这些字段。

No matter what, your logic says to disable those fields. 无论如何,您的逻辑说禁用这些字段。

It might make things easier to use property binding to control the disabled state of your fields. 使用属性绑定来控制字段的禁用状态可能会更容易。 If the radio button is checked, then you can enable the fields and if not, disable them using the selectedProperty() of the radio button. 如果选中了单选按钮,则可以启用这些字段;如果未选中,请使用单选按钮的selectedProperty()禁用它们。

Something like this (in your initialize method, or similar): 这样的东西(在您的initialize方法中,或类似的东西):

rdDelivery.selectedProperty().bind(Bindings.not(txtAddress.disabledProperty()));
rdDelivery.selectedProperty().bind(Bindings.not(txtEmail.disabledProperty()));

You've written the code to disable them when it is checked, but you haven't done anything to handle when it is unchecked. 您已经编写了禁用它们的代码,但是未选中时您没有做任何处理。 You cannot expect the computer to infer this. 您不能指望计算机可以推断出这一点。

You need to listen for when the button is unchecked and respond appropriately. 您需要在未选中按钮时进行侦听,并做出适当的响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM