javafx fxml ComboBox Error

前端 未结 2 1213
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-14 21:28

Im trying to add a String to a javafx comboBox but i keep getting the above error :/

no suitable method found for add(String)
method Collection.add(CAP#         


        
相关标签:
2条回答
  • 2021-01-14 22:06

    this is because the ComboBox elements type is not set so it's by defauld "?". like this :

    ComboBox<?> room_id = new ComboBox<>(); 
    

    so to force the fxml ComboBox to have String values you have to add something like this :

    <ComboBox fx:id="cbo_Bacteriologie_Aesculine" prefHeight="21.0" prefWidth="105.0" GridPane.columnIndex="1" GridPane.rowIndex="0">
        <items>
            <FXCollections fx:factory="observableArrayList">
                <String fx:value="string option" />
            </FXCollections>
        </items>
    </ComboBox>
    

    or set the observable list from code like this :

    Java ComboBox .setItems (ObservableList < T > value)

    0 讨论(0)
  • 2021-01-14 22:12

    Simply declare the room_id field in your controller class as

    @FXML
    private ComboBox<String> room_id;
    

    If you're using

    @FXML
    private ComboBox<?> room_id;
    

    room_id.getItems() returns a ObservableList<?> i.e. a ObservableList with unknown element type and String cannot be assigned to this type.

    0 讨论(0)
提交回复
热议问题