javafx fxml ComboBox 错误 [英] javafx fxml ComboBox Error

查看:55
本文介绍了javafx fxml ComboBox 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串添加到 javafx 组合框,但我不断收到上述错误:/

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 ?

代码

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

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

After using a list, i get

不兼容的类型:字符串不能转换为 CAP#1
其中 CAP#1 是一个新的类型变量:
CAP#1 从 ?

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<?>,即一个具有未知元素类型和 StringObservableList代码>不能分配给这种类型.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆