选择JFXCombobox项目并发送到新的Combobox

时间:2018-02-09 15:56:22

标签: java javafx lambda java-8 jfoenix

我将JFoenix库用于我的Comboboxes。

' boxLeague.getSelectionModel()。selectedItemProperty()。addListener((observable,oldValue,newValue) - > boxTeams.setItems(listPremierLeague)); '将当从boxLeague Combobox中选择任何内容时,将所有文本放到boxTeams Combobox中,但我想要做的是当在boxLeague中选择特定项目时,然后填充另一个组合框。

public class Controller implements Initializable {

@FXML
private JFXComboBox<String> boxLeague;

@FXML
private JFXComboBox<String> boxTeams;

@FXML
private JFXComboBox<String> boxPlayers;


ObservableList<String> listLeagues = FXCollections.observableArrayList(
        "Bundesliga", "La Liga", "Ligue 1", "Premier League", "Serie A", "Champions League", "Europa League");

ObservableList<String> listPremierLeague = FXCollections.observableArrayList(
        "Arsenal", "Bournemouth", "Brighton", "Burnley", "Chelsea", "Crystal Palace", "Everton");




@Override
public void initialize(URL location, ResourceBundle resources) {

    boxLeague.setItems(listLeagues);
    boxLeague.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> boxTeams.setItems(listPremierLeague));
}

}

1 个答案:

答案 0 :(得分:5)

在你的听众内部,你需要找出哪个联盟被选中了boxTeams的项目。

boxLeague.getSelectionModel().selectedItemProperty().addListener(
  (observable, oldValue, newValue) -> { 
      if (newValue.equals("Premier League")) {
          boxTeams.setItems(listPremierLeague));
      } // else if ... (or use a switch-case here)
  }
);

请注意,如果您不会将String用于联赛和球队,但可以创建自己的课程,则可以进一步改进。