簡體   English   中英

組合框依賴於另一個組合框 - JavaFX

[英]combobox dependent on another combobox - JavaFX

我有三個組合框:國家、州和城市

我怎么可能成為另一個人的依賴者? 例如,如果我選擇巴西,則會出現他們的州,然后是所選州的城市。 但是如果我在國家選擇美國會顯示他們的州

我用的是mysql作為數據庫,如果你需要在數據庫中進行一些配置也告訴我......這是你第一次使用它,非常感謝。

向國家組合框注冊一個監聽器,並在所選項目發生變化時更新狀態組合框:

cbxCountry.valueProperty().addListener((obs, oldValue, newValue) -> {
    if (newValue == null) {
        cbxState.getItems().clear();
        cbxState.setDisable(true);
    } else {
        // sample code, adapt as needed:
        List<State> states = stateDAO.getStatesForCountry(newValue);
        cbxState.getItems().setAll(states);
        cbxState.setDisable(false);
    }
});

如果您願意,也可以使用綁定來執行此操作:

cbxState.itemsProperty().bind(Bindings.createObjectBinding(() -> {
    Country country = cbxCountry.getValue();
    if (country == null) {
        return FXCollections.observableArrayList();
    } else {
        List<State> states = stateDAO.getStatesForCountry(country);
        return FXCollections.observableArrayList(states);
    }
},
cbxCountry.valueProperty());

(如果您想要上述解決方案中的禁用功能,也可以執行cbxState.disableProperty().bind(cbxCountry.valueProperty().isNull()); )。

暫無
暫無

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

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