将项添加到comboBox javaFx FXML时出错

时间:2017-12-31 09:44:19

标签: java arrays javafx combobox 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代码

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文件

<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());


IN FACT 当我连投身时,

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

它仍会显示相同的错误,这个问题似乎与我从scenebuilder JavaFX创建的comboBox有关

1 个答案:

答案 0 :(得分:0)

您应该使用mysqli_query($conn, $sql); if (!mysqli_query($conn, $sql)) { print "Error: " . mysqli_error($conn); error_log("SQL query error: " . mysqli_error($conn)); } 来解决问题,而不是使用room_id.getItems().add(String.valueOf(i));

你能展示样品JSON吗?

这是工作样本控制器

room_id.getItems().add(itemIDS.get(i));

和FXML

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;
    }
}

此处示例截图

enter image description here

相关问题