1

I want to create an application with FXML with two custom controls, where one of them needs a reference to the other.

The first class gets the reference to the second class via a constructor argument. This is also available in the FXML with the @NamedArgs annotation.

public SelectWeekControl(@NamedArg("dayControl") SelectDayControl selectDayControl) {
    this.selectDayControl = selectDayControl;
    ...
}

Now I want to add both to the FXML file by referencing the @NamedArgs field with the fx:id of the other component.

<SelectDayControl fx:id="daySelect"/>
<SelectWeekControl dayControl="${daySelect}" fx:id="weekSelect"/>

Unfortunately the second line fails with the following error message:

Caused by: javafx.fxml.LoadException: Cannot bind to untyped object.

The only other question referring to the same issue has an unsatisfying answer.

Jibbow
  • 757
  • 1
  • 8
  • 17

1 Answers1

2

I don't know why the code you tried doesn't work, but the following version seems to work ok:

<SelectWeekControl fx:id="selectWeekControl">
    <selectDayControl>
        <SelectDayControl fx:id="selectDayControl" />
    </selectDayControl>
</SelectWeekControl>

and (if you need the SelectDayControl defined elsewhere in the FXML file) so does

<SelectDayControl fx:id="selectDayControl" />

<SelectWeekControl fx:id="selectWeekControl">
    <selectDayControl>
        <fx:reference source="selectDayControl" />
    </selectDayControl>
</SelectWeekControl>
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Cool, thank you, it works! But I would like to add some more information for other people with the same issue: `SelectDayControl` has to be defined *before* `SelectWeekControl` and although the second approach is valid, IntelliJ highlights it in red and says "cannot resolve symbol" – Jibbow Oct 31 '17 at 08:01