向国家组合框注册一个监听器,并在所选项目发生变化时更新状态组合框:
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);
}
});
Run Code Online (Sandbox Code Playgroud)
如果您愿意,也可以使用绑定来执行此操作:
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());
Run Code Online (Sandbox Code Playgroud)
(如果您想要上述解决方案中的禁用功能,也可以这样做cbxState.disableProperty().bind(cbxCountry.valueProperty().isNull());)。
| 归档时间: |
|
| 查看次数: |
2051 次 |
| 最近记录: |