2013-09-28 3 views
7

javaFx의 콤보 상자에 채울 값 목록이 있습니다. 이것은이 내 combo.xmlJavaFx에서 콤보 상자에 목록 값을 채우는 방법

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="- Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2"> 
<children> 
<ComboBox id="comboId" layoutX="210.0" layoutY="108.0" prefHeight="27.0" prefWidth="102.0" promptText="Select"> 
    <items> 
     <FXCollections fx:factory="observableArrayList"> 
     <String fx:value="Item 1" /> 
     <String fx:value="Item 2" /> 
     <String fx:value="Item 3" /> 
     </FXCollections> 
    </items> 
    </Com boBox> 
    </children> 
    </AnchorPane> 

내 주요

public class JavaFXExperiment extends Application { 
@Override 
public void start(Stage stage) throws Exception { 
    Parent root = FXMLLoader.load(getClass().getResource("combo.fxml")); 
    Scene scene = new Scene(root); 
    stage.setScene(scene); 
    stage.show(); 
    final ComboBox comboId = new ComboBox(); 
    comboId.getItems().addAll(
      "[email protected]", 
      "[email protected]", 
      "ethan.wil[email protected]", 
      "[email protected]", 
      "[email protected]"); 
} 
    public static void main(String[] args) { 
    launch(args); 
} 
} 

이 내 xml 파일과 내가 combobox.anyone에 그 값을 표시 할 기본 클래스 도와주세요입니다

답변

18

컨트롤러 하나를 만들고 FXML 화면에 할당해야합니다. 새 창을 열 때

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="- Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" fx:controller="MyController" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2"> 
<children> 
<ComboBox fx:id="myCombobox" id="comboId" layoutX="210.0" layoutY="108.0" prefHeight="27.0" prefWidth="102.0" promptText="Select"> 
    <items> 
     <FXCollections fx:factory="observableArrayList"> 
     <String fx:value="Item 1" /> 
     <String fx:value="Item 2" /> 
     <String fx:value="Item 3" /> 
     </FXCollections> 
    </items> 
    </ComboBox> 
    </children> 
    </AnchorPane> 

그런 다음 메인 클래스는,

public class JavaFXExperiment extends Application { 
@Override 
public void start(Stage stage) throws Exception { 

    FXMLLoader loader = new FXMLLoader(getClass().getResource("combo.fxml")); 
    Parent root = loader.load(); 

    MyController myController = loader.getController(); 

    Scene scene = new Scene(root); 
    stage.setScene(scene); 
    stage.show(); 

    //Set Data to FXML through controller 
    myController.setData(); 
} 
    public static void main(String[] args) { 
    launch(args); 
} 
} 

될 것입니다 그리고 당신의 컨트롤러가 될

public class MyController implements Initializable 
{ 

@FXML 
public Combobox myCombobox; 

@Override 
    public void initialize(URL url, ResourceBundle rb) { 
} 

public void setData(){ 

myCombobox.getItems().clear(); 

myCombobox.getItems().addAll(
      "[email protected]", 
      "[email protected]", 
      "[email protected]", 
      "[email protected]", 
      "[email protected]"); 

} 
} 
+0

감사합니다,이 또한 작동합니다. – lmiguelvargasf

+0

'@FXML 공용 ComboBox myCombobox; – saikosen

관련 문제