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
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");
}
}
When creating a combo box, you must instantiate the ComboBox class and define the items as an observable list, just like other UI controls such as ChoiceBox, ListView, and TableView
sets the items within a constructor.
ObservableList<String> options =
FXCollections.observableArrayList(
"Option 1",
"Option 2",
"Option 3"
);
final ComboBox comboBox = new ComboBox(options);