2016-10-10 3 views
0

세 개의 콤보 상자가 있습니다. 국가, 주 및 도시콤보 상자가 다른 콤보 상자에 종속 됨 - 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()); 

를 (선택 항목이 변경 될 때

+0

Obs : 콤보 박스를 채우는 예제 public void country() { listCountry = countryDAO.show(); observableListCountry = FXCollections.observableArrayList (listCountry); cbxCountry.setItems (observableList); } – Junior

+1

주석에 코드를 게시하지 마십시오. 질문을 편집하고 거기에 추가하십시오. –

답변

1

상태 콤보 상자를 국가 콤보 상자에 리스너를 등록 및 업데이트 위의 솔루션에서 비활성화 기능을 원할 경우 cbxState.disableProperty().bind(cbxCountry.valueProperty().isNull());을 수행하십시오.

관련 문제