I have this FXML file, where I tried to populate the ComboBox:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="650.0" minWidth="750.0" prefHeight="700.0" prefWidth="822.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="table.Table">
<children>
<MenuButton fx:id="dateFilter" layoutX="6.0" layoutY="55.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="114.0" text="Date" />
<ComboBox fx:id="descriptionFilter" editable="true" layoutX="226.0" layoutY="55.0" prefHeight="25.0" prefWidth="204.0" promptText="Series Description">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="1" />
<String fx:value="20" />
<String fx:value="300" />
</FXCollections>
</items>
</ComboBox>
</children>
</AnchorPane>
But since I populated it, it won't open on SceneBuilder and it displays this error:
Error
java.io.IOException: javafx.fxml.LoadException: Invalid attribute. /C:/Users/BTAP/workspace/Tst/src/table/table.fxml:12
And it won't load my Application:
Caused by: javafx.fxml.LoadException: FXCollections is not a valid type.
if I remove the fx:factory="observableArrayList"
it loads on the scene builder and show a warning but still won't run my program.
And I don't quite understand since it's the same way as I saw in many examples example1, example2, example3.
Why am I getting this error? Shouldn't it work?
I know how to fill the elements by code, but I am looking for a FXML solution.
You can't add items to combobox through SceneBuilder. Either you can add through FXML file as you did or through controller as given below. I suggest calling: comboBox. getItems().
JavaFX ComboBox is an implementation of simple ComboBox which shows a list of items out of which user can select at most one item, it inherits the class ComboBoxBase. Constructors of ComboBox: ComboBox(): creates a default empty combo box. ComboBox(ObservableList i): creates a combo box with the given items.
It is also possible to assign the controller class directly from the FXML file. In order to do that, you should first open your FXML file and add the following code on the first line of the file right after declarations. Example: Since my controller is inside the package name “view”, my fx: controller = “view.
You need an import for the FXCollections
class:
<?import javafx.collections.FXCollections ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With