3

我将 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));
}

}

4

1 回答 1

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请注意,如果您不将s 用于联赛和球队,而是创建自己的类,则可以进一步改进。

于 2018-02-09T16:12:15.970 回答