Reputation: 35
I am fairly new to JavaFX and I am trying to figure out how exactly the link between an FXML file and the controller works. My problem is that I am using a CheckComboBox from ControlsFX that needs to get its set of Strings for the possible options within the ComboBox when the objects gets created.
private void initializeComboBoxes() {
final ObservableList<String> projects = FXCollections.observableArrayList();
for (String x : db.getProjectBag()) {
projects.add(x);
}
comboBoxProject = new CheckComboBox<String>(projects);
...
The problem is that when I set the FXML-linked CheckedComboBox field to that new object (as far as I'm concerned that needs to be done every time when the possible options for it change e.g. based on values from a database), it doesn't show up on the user interface.
Maybe I am missing out how to dynamically link the user interface controls to their underlying objects, but help would be very appreciated.
Best regards,
PWiese
Upvotes: 1
Views: 53
Reputation: 159466
Never create a new object in Java for an object which is already created by the FXMLLoader
(any object which has a reference marked @FXML
in your controller).
By default the combobox will be created with an empty list of items, just get that list and set the values you wish on it.
For example, use the following code in your controller code, invoking the initializeComboBox()
method as needed:
@FXML
CheckComboBox comboBoxProject;
private void initializeComboBox() {
final List<String> projects = new ArrayList();
for (String x : db.getProjectBag()) {
projects.add(x);
}
comboBoxProject.getItems().setAll(projects);
}
Upvotes: 2