1

how do one pass a non-string object as argment using @namedArg in javafx? I cannot find a single nutshell example regarding this question online!

I am currently trying to instantiate a InlineCssTextArea from RichTextFX wrapped in a VirtualizedScrollPane - please have a look at this source code:

public VirtualizedScrollPane(@NamedArg("content") V content) {
  [...]
}

where the custom type V is extending Node. In my case, I want to pass InlineCssTextArea as V. Doing this programmatically is pretty easy:

InlineCssTextArea area = new InlineCssTextArea();
Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(area)), 600, 400);

but translating that to FXML is quite challenging. I have already tried a few things, like fx:factory based on the official oracle fxml tutorial:

<VirtualizedScrollPane fx:factory="content">
  <InlineCssTextArea />
</VirtualizedScrollPane>

or how @namedArg suggests, as argument:

<VirtualizedScrollPane content="InlineCssTextArea" />

-or-

<VirtualizedScrollPane content="<InlineCssTextArea />" />

Is there a fxml solution for this problem?

my question is based on the the following answer from James D: What is the purpose of @NamedArg annotation in javaFX 8?

Adrian
  • 11
  • 2

1 Answers1

1

You basically need to pass a value for an argument called content. The two ways to pass a value for an argument in FXML are as an attribute: content="..." or using a property element. Using an attribute only works if you can pass a string which the FXML loader knows how to convert to the appropriate value (i.e. if the value is a string or a primitive type), which isn't the case here. Using a property element you just nest an element whose name is the property name and nest the value inside it:

<VirtualizedScrollPane>
    <content>
        <InlineCssTextArea />
    </content>
</VirtualizedScrollPane>
James_D
  • 201,275
  • 16
  • 291
  • 322
  • unfortunately, this approach does not work. I get an error while loading the fxml file. [java.lang.reflect.InvocationTargetException] – Adrian Apr 29 '17 at 12:27
  • @Adrian Actually: what is a `VirtualizedScrollPane` (which package is it in)? I can't find it the RichTextFX documentation. – James_D Apr 29 '17 at 12:51
  • thanks for looking into it! here is the URL of the source code: https://github.com/TomasMikula/Flowless/blob/master/src/main/java/org/fxmisc/flowless/VirtualizedScrollPane.java – Adrian Apr 29 '17 at 13:00
  • Hmm. Well, it should work; I created a dummy `VirtualizedScrollPane` class with the same constructors and it worked fine, but it's not working with the jar file I downloaded from flowless. The addition of the `@FXML` is a recent change: I wonder if that change hasn't made it to the snapshot yet. How are you installing the dependency on flowless, and which version are you using? – James_D Apr 29 '17 at 13:25
  • Ahh alright, now I understand. I went through the source code of the flawless jar and did not find any namedArg annotations - apparently they did not make it to the build yet. Thank you very much for the help! – Adrian Apr 29 '17 at 14:27
  • @Adrian If you're feeling adventurous, you could try to build it yourself from the current source... – James_D Apr 29 '17 at 14:29