将项添加到comboBox javaFx FXML时出错[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将项添加到comboBox javaFx FXML时出错[重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我试图将ArrayList中的所有项添加到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 ?
Java Code
File fJSon = new File("roomDetails.json");
JSONParser parser = new JSONParser();
try{
Object obj = parser.parse(new FileReader("roomDetails.json"));
JSONObject jsonObject = (JSONObject) obj;
List<String> itemIDS = new ArrayList<String> (jsonObject.keySet()); //gets json keys from a json oject previously defined
for (int i = 0; i < itemIDS.size(); i++) {
room_id.getItems().add(String.valueOf(i));
}
FXML file
<AnchorPane id="AnchorPane" prefHeight="437.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="pp01_cwk03.AddStaffController">
<children>
<Pane layoutX="14.0" layoutY="5.0" prefHeight="428.0"
prefWidth="593.0">
<children>
<ComboBox fx:id="room_id" layoutX="187.0" layoutY="178.0" prefHeight="25.0" prefWidth="149.0" />
</children>
</Pane>
</children>
</Pane>
</children>
roomDetails.json
{"366O":
["Room Name:CEO",
"Department Name:IT",
"Occupant Name:Charindu",
"Space Type:Office"],
"527F":
["Room Name:IT Lab",
"Department Name:IT",
"Occupant Name:Saman",
"Space Type: Library"]
}
从json对象获取KEYS的代码可以工作,因为我使用for循环测试了它自己,打印了数组细节
List<String> itemIDS = new ArrayList<String> (jsonObject.keySet());
事实当我甚至放,
room_id.getItems().add("Hello");
它仍然会显示相同的错误,这个问题似乎与我从scenebuilder JavaFX创建的comboBox有关
答案
而不是使用room_id.getItems().add(String.valueOf(i));
你应该使用room_id.getItems().add(itemIDS.get(i));
来解决问题。
你能展示样品JSON吗?
这是工作样本控制器
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;
import org.json.JSONObject;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
public class ComboTestController implements Initializable {
Stage primaryStage;
public ComboBox comboBox;
/**
* Called to initialize a controller after its root element has been
* completely processed.
*
* @param location The location used to resolve relative paths for the root object, or
* <tt>null</tt> if the location is not known.
* @param resources The resources used to localize the root object, or <tt>null</tt> if
*/
public void initialize(URL location, ResourceBundle resources) {
JSONObject jsonObject = new JSONObject();
jsonObject.append("Key1", "Value 1");
jsonObject.append("Key2", "Value 2");
jsonObject.append("Key3", "Value 3");
List<String> itemIDS = new ArrayList<String>(jsonObject.keySet()); //gets json keys from a json oject previously defined
for (int i = 0; i < itemIDS.size(); i++) {
comboBox.getItems().add(itemIDS.get(i));
}
}
public void init(Stage primaryStage) {
this.primaryStage = primaryStage;
}
}
和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="comboBox" layoutX="170.0" layoutY="185.0" prefHeight="31.0" prefWidth="260.0" />
</children>
</AnchorPane>
这里有示例截图
以上是关于将项添加到comboBox javaFx FXML时出错[重复]的主要内容,如果未能解决你的问题,请参考以下文章
JavaFX / FXML 将 ChoiceBox 添加到根窗格