簡體   English   中英

(JavaFX)綁定textProperty()時無法使用控件嗎?

[英](JavaFX) Unable to use controls when binding the textProperty()?

我剛剛開始學習屬性綁定。 我設置了控件,並在此處綁定了屬性(代碼縮寫):

@FXML
    private TextField txtOldPremium;
    @FXML
    private TextField txtNewPremium;
    @FXML
    private TextField txtProratedChange;

    private SimpleBooleanProperty quoteOnly = new SimpleBooleanProperty();
    private SimpleBooleanProperty reviewedBilling = new SimpleBooleanProperty();
    private SimpleStringProperty oldPremium = new SimpleStringProperty();
    private SimpleStringProperty newPremium = new SimpleStringProperty();
    private SimpleStringProperty proratedChange = new SimpleStringProperty();
    private SimpleStringProperty additionalInformation = new SimpleStringProperty();

    @FXML
    public void initialize() {

        // Bind the vehicle data to the tableview
        colEffectiveDate.setCellValueFactory(new PropertyValueFactory<Vehicle, String>("effectiveDateString"));
        colDescription.setCellValueFactory(new PropertyValueFactory<Vehicle, String>("description"));
        colVIN.setCellValueFactory(new PropertyValueFactory<Vehicle, String>("vin"));
        colAction.setCellValueFactory(new PropertyValueFactory<Vehicle, String>("action"));

        // Setup data binding to controls
        chkQuoteOnly.selectedProperty().bindBidirectional(quoteOnly);
        chkReviewedBilling.selectedProperty().bindBidirectional(reviewedBilling);
        txtOldPremium.textProperty().bindBidirectional(oldPremium);
        txtNewPremium.textProperty().bindBidirectional(newPremium);
    }

    @Override
    public String getComments() {
        StringBuilder comments = new StringBuilder();

        if (!txtOldPremium.getText().isEmpty() && !txtNewPremium.getText().isEmpty()) {

        } else {

            }
        }
        if (!txtProratedChange.getText().isEmpty()) {

        }

我在if (!txtOldPremium.getText().isEmpty() && !txtNewPremium.getText().isEmpty()) {時收到NullPointerException。 我假設我在實現綁定方面做的根本錯誤。 TextFields本身當前為空。

基本上,我只希望TextFieldoldPremium String始終保持彼此同步。 但是我仍然需要能夠對文本TextField進行處理。 我確實還有其他幾種類型的控件( CheckBoxComboBox等),它們希望與基礎數據同步。

您正在將文本字段的文本屬性綁定到沒有初始值創建的StringProperty實例。 因此,例如,當您

private SimpleStringProperty oldPremium = new SimpleStringProperty();

您創建的oldPremium沒有實際值。 因此oldPremium.getValue()將返回null

所以當你這樣做

txtOldPremium.textProperty().bindBidirectional(oldPremium);

您需要同步兩個屬性( txtOldPremium.textProperty()oldPremium )的狀態,這涉及將oldPremium的值(為null )復制到txtOldPremium.textProperty()

因此,只需初始化屬性即可容納一個空字符串,而不是null:

private SimpleStringProperty oldPremium = new SimpleStringProperty("");

等等

此外,盡管如此,請注意,在此特定示例中,您可以實現相同的操作而無需復制所有屬性。 即你可以做

private StringProperty oldPremium ;

// ...

@FXML
public void initialize() {

    // ...

    oldPremium = txtOldPremium.textProperty();

    // ...

}

您仍然可以定義屬性訪問器方法

public StringProperty oldPremiumProperty() {
    return oldPremium ;
}

public final String getOldPremium() {
    return oldPremiumProperty().get();
}

public final void setOldPremium(String oldPremium) {
    oldPremiumProperty().set(oldPremium);
}

正如您可能已經擁有的那樣,它將具有完全相同的效果,但開銷卻更少。

雙向綁定對於同步屬於兩個不同對象的屬性(例如,屬於不同控制器,或控制器和模型的屬性)確實非常有用。 也許您的問題簡化了代碼來演示問題,但是我想我會為其他用戶指出這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM