Javafx:如何在checkcombobox中添加itmes?

时间:2017-09-21 06:12:09

标签: java javafx fxml

我正在尝试在checkComboBox中添加itmes,但我不知道为什么我没有这样做。以下是我要做的事情:

`// 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());
         }
     });
     }

1 个答案:

答案 0 :(得分:1)

本准则正常运作

我的fxml代码

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

我的控制器代码:

//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();
         }
     });
 }