2017-09-21 3 views
-1

J'essaie d'ajouter des itmes dans mon checkComboBox mais je ne sais pas pourquoi je ne le fais pas. Voici ce que je suis en train de faire ceci:Javafx: Comment ajouter des itmes dans checkcombobox?

`// initialinzing FXML in my controller` 
@FXML 
CheckComboBox<String> checkComboBox; 

// create the data to show in the CheckComboBox 
    final ObservableList<String> strings = FXCollections.observableArrayList(); 
    for (int i = 0; i <= 10; i++) { 
     strings.add("Item " + i); 
    } 

    // Create the CheckComboBox with the data 
    checkComboBox = new CheckComboBox<String>(strings); 


    // and listen to the relevant events (e.g. when the selected indices or 
    // selected items change). 
    checkComboBox.getCheckModel().getSelectedItems().addListener(new ListChangeListener<String>() { 
     public void onChanged(ListChangeListener.Change<? extends String> c) { 
      System.out.println(checkComboBox.getCheckModel().getSelectedItems()); 
     } 
    }); 
    } 
+0

Il est fait. juste fait un petit changement tout en ajoutant ma liste observable dans mon checkcombobox. checkComboBox.getItems(). AddAll (chaînes); istead de checkComboBox = new CheckComboBox (chaînes de caractères); –

+0

Pourquoi créez-vous un nouveau 'CheckComboBox'? L'ajoutez-vous à la scène n'importe où? –

+0

Oui! J'ai ajouté 'checkComboBox' dans ma scène. C'est là que je me trompais en créant un nouveau 'checkComboBox'. Au lieu de faire un nouveau checkComboBox je dois appeler directement la fonction 'getItems(). AddAll (strings)' pour ajouter mon ObservableList dans mon checkComboBox –

Répondre

0

Ce code fonctionne bien

Mon code FXML

<CheckComboBox fx:id="addFeaturesCheckComboBox" prefHeight="25.0" prefWidth="192.0" GridPane.columnIndex="1" GridPane.rowIndex="2" /> 

Mon code Contrôleur:

//to initialize my checkComboBox 
@FXML 
CheckComboBox<String> addFeaturesCheckComboBox; 

public void initialize() throws SQLException{ 

ObservableList<String> strings = FXCollections.observableArrayList(); 
    for (int i = 0; i <= 10; i++) { 
     strings.add("Item " + i); 
    } 
addFeaturesCheckComboBox.getItems().addAll(strings); 
//listen to the relevant events (e.g. when the selected indices or 
    // selected items change). 

    addFeaturesCheckComboBox.getCheckModel().getSelectedItems().addListener(new ListChangeListener<String>() { 
     public void onChanged(ListChangeListener.Change<? extends String> c) { 

      selectedFeatures = addFeaturesCheckComboBox.getCheckModel().getSelectedItems(); 
     } 
    }); 
}