javafx fxml ComboBox错误 [英] javafx fxml ComboBox Error
问题描述
我试图将一个字符串添加到javafx comboBox但我一直收到上述错误:/
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#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 ?
代码
CODE
room_id.getItems().add("Hello");
FXML
FXML
<?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>
UPDATE
使用清单后,我得到
UPDATE
After using a list, i get
不兼容的类型:字符串无法转换为CAP#1
其中CAP#1是一个新的类型变量:
CAP#1从捕获中扩展Object?
incompatible types: String cannot be converted to CAP#1
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
ObservableList<String> list=FXCollections.observableArrayList("1","2","3","4");
room_id.setItems(list);
推荐答案
只需声明 room_id
控制器类中的字段为
Simply declare the room_id
field in your controller class as
@FXML
private ComboBox<String> room_id;
如果你正在使用
@FXML
private ComboBox<?> room_id;
room_id.getItems()
返回 ObservableList<?>
即具有未知元素类型的 ObservableList
和 String
无法分配到此类型。
room_id.getItems()
returns a ObservableList<?>
i.e. a ObservableList
with unknown element type and String
cannot be assigned to this type.
这篇关于javafx fxml ComboBox错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!