2017-12-31 46 views
0

Im Versuch, alle Elemente in einem Arraylist zu einem javafx ComboBox hinzuzufügen, aber ich halte einen wierd Fehler bekommen,Fehler beim Hinzufügen Artikel zu comboBox JavaFX FXML

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 Datei

<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"] 
} 

Dieser Code, der die Schlüssel aus dem json bekommt oject arbeitet becouse ich es mir selbst mit einer for-Schleife, die die Details Array druckt getestet

List<String> itemIDS = new ArrayList<String> (jsonObject.keySet()); 


TATSÄCHLICH Wenn ich sogar setze,

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

Es würde immer noch den gleichen Fehler zeigen, das Problem einige wh at scheint mit der ComboBox ich von Scenebuilder erstellt JavaFX

Antwort

0

Anstelle von room_id.getItems().add(String.valueOf(i)); sollten Sie room_id.getItems().add(itemIDS.get(i)); verwenden, die das Problem lösen sollte.

Können Sie das Beispiel JSON zeigen? Hier

ist der Arbeitsbeispiel-Controller

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

Und 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> 

Hier Beispiel-Screenshot

enter image description here

+0

Dennoch i halten den gleichen Fehler:/ –

+0

Don‘ t verwende rohe Typen. –