2013-09-28 23 views
7

Ich habe eine Liste von Werten, die ich in einer Combobox in JavaFx auffüllen möchte. dies ist mein combo.xmlSo füllen Sie Listenwerte in einer Combobox in JavaFx

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

das ist mein Haupt

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]", 
      "[email protected]", 
      "[email protected]", 
      "[email protected]"); 
} 
    public static void main(String[] args) { 
    launch(args); 
} 
} 

Dies ist meine XML-Datei und die Hauptklasse i bitte helfen, diese Werte in der combobox.anyone zeigen wollen

Antwort

18

Sie müssen einen Controller erstellen und diesen mit Ihrem FXML-Bildschirm zuweisen.

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

Dann ist Ihre Hauptklasse wird,

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

Und Ihr Controller wird,

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

Danke, das funktioniert auch, wenn ein neues Fenster zu öffnen. – lmiguelvargasf

+0

'@FXML öffentliche ComboBox myCombobox;' – saikosen

Verwandte Themen