代码之家  ›  专栏  ›  技术社区  ›  DMT82

选择JFXCombobox项并发送到新的Combobox

  •  3
  • DMT82  · 技术社区  · 8 年前

    我将JFoenix库用于组合框。

    ' boxLeague。getSelectionModel()。选择EditEmProperty()。addListener(可观察、oldValue、newValue)->BoxTeam。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));
    }
    

    }

    1 回复  |  直到 8 年前
        1
  •  5
  •   Markus K    8 年前

    在你的听众中,你需要找出哪个联赛被选中,并设置 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 代表联赛和球队,但创建自己的课程。