How to populate a list values to a combobox in JavaFx


Question

I have a list of values which i want to populate in a combobox in javaFx. this is my combo.xml

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

this is my main

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(
            "jacob.smith@example.com",
            "isabella.johnson@example.com",
            "ethan.williams@example.com",
            "emma.jones@example.com",
            "michael.brown@example.com");
}
  public static void main(String[] args) {
    launch(args);
}
}

This is my xml file and the main class i want to show those values in the combobox.anyone please help

1
9
9/28/2013 9:35:42 AM

You have to create one controller and assign it with your FXML Screen.

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

Then your main class will be,

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

And Your controller will be,

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(
            "jacob.smith@example.com",
            "isabella.johnson@example.com",
            "ethan.williams@example.com",
            "emma.jones@example.com",
            "michael.brown@example.com");

}
}
25
12/1/2016 8:50:22 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon