2017-12-31 136 views
1

임 내가 위의 오류가 계속하지만, 자바 FX 콤보 상자에 문자열을 추가하려고 :/
자바 FX 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 ? 

CODE

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> 

업데이트

목록을 사용한 후, 난

호환되지 않는 유형을 얻을 : 문자열 #을 CAP로 변환 할 수없는 CAP # 1은 새로운 유형의 변수 1
:
CAP # 1이 캡처에서 객체를 확장은?

ObservableList<String> list=FXCollections.observableArrayList("1","2","3","4"); 

room_id.setItems(list); 
+0

다른 컴파일러에서 코드를 컴파일 해보십시오! –

+0

컨트롤러에 'room_id'선언을 표시 할 수 있습니까? –

답변

3

컨트롤러 클래스의 room_id 필드를

로 간단히 선언하십시오. 16,
@FXML 
private ComboBox<String> room_id; 

당신이

@FXML 
private ComboBox<?> room_id; 

room_id.getItems()을 사용하는 경우는 즉, 알 수없는 요소 유형 및 StringObservableListObservableList<?>이 유형에 할당 할 수 없습니다를 반환합니다.

+1

질문을 업데이트했다. 6 시간 후에, 대답은 작동한다! 감사합니다 <3 –

0

이것은 ComboBox 요소 유형이 설정되지 않았기 때문에 defauld "?"입니다. 이 같은 :

<ComboBox fx:id="cbo_Bacteriologie_Aesculine" prefHeight="21.0" prefWidth="105.0" GridPane.columnIndex="1" GridPane.rowIndex="0"> 
    <items> 
     <FXCollections fx:factory="observableArrayList"> 
      <String fx:value="string option" /> 
     </FXCollections> 
    </items> 
</ComboBox> 

또는 코드에서이 같은 관측 목록 설정 :

ComboBox<?> room_id = new ComboBox<>(); 

그래서 문자열이 같은 것을 추가해야 값이 할 수 fxml 콤보 상자를 강제로 Java ComboBox .setItems (ObservableList < T > value)

+0

코드로 상품을 설정해야합니다. –

+0

예, 답변에 표시된 것과 같이 FXCollections 태그를 추가하고 동일한 코드를 다시 시도하십시오! –

+0

<문자열 fx : 값 = "문자열 옵션"/>을 추가 했습니까? –