2016-10-10 57 views
0

我有三个组合框:国家,州和城市combobox依赖于另一个组合框 - JavaFX

我怎样才能成为一个依赖另一个?例如,如果我选择巴西出现他们的州和后来的选定州的城市。但如果我选择美国在该国将显示其国家

我使用MySQL作为数据库,如果你需要在数据库中的某些配置也告诉我......这是你第一次使用它,谢谢你非常。

+0

Obs:我如何填充组合框的示例 public void country(){ listCountry = countryDAO.show(); observableListCountry = FXCollections.observableArrayList(listCountry); cbxCountry.setItems(observableList); } – Junior

+1

请不要在评论中发布代码:[编辑]你的问题,并将其添加到那里。 –

回答

1

注册与国家组合框的监听器和更新状态组合框时,所选项目的变化:

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());)。