Select JFXCombobox 项目并发送到新的 Combobox

Select JFXCombobox item and send to new Combobox

我将 JFoenix 库用于组合框。

'boxLeague.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> boxTeams.setItems(listPremierLeague));' 当从 boxLeague 组合框中选择任何内容时,会将所有文本放入 boxTeams 组合框,但我想要做的是,当在 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));
}

}

在您的侦听器内部,您需要找出选择了哪个联赛并相应地设置 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,而是创建您自己的 类,这可能会进一步改进。