javafx fxml ComboBox错误

Charindu Edirisooriya

我试图将字符串添加到javafx comboBox,但我不断收到上述错误:/

no suitable method found for add(String)
method Collection.add(CAP#1) is not applicable
  (argument mismatch; String cannot be converted to CAP#1)
method List.add(CAP#1) is not applicable
  (argument mismatch; String cannot be converted to CAP#1)
   where CAP#1 is a fresh type-variable:
  CAP#1 extends Object from capture of ?

room_id.getItems().add("Hello");

XML文件

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.AutoMaven.ui.controller.ComboTestController">
   <children>
      <ComboBox fx:id="room_id" layoutX="170.0" layoutY="185.0" prefHeight="31.0" prefWidth="260.0" />
   </children>
</AnchorPane>

更新

使用列表后,我得到

不兼容的类型:不能将字符串转换为CAP#1
,其中CAP#1是新的类型变量:
CAP#1扩展了对象的捕获?

ObservableList<String> list=FXCollections.observableArrayList("1","2","3","4");

room_id.setItems(list);
法比安

只需room_id在控制器类中将字段声明

@FXML
private ComboBox<String> room_id;

如果您正在使用

@FXML
private ComboBox<?> room_id;

room_id.getItems()返回具有未知元素类型的a,ObservableList<?>不能分配给该类型。ObservableListString

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章