Forum Stats

  • 3,874,141 Users
  • 2,266,672 Discussions
  • 7,911,738 Comments

Discussions

How to access a non-String-Object from ResourceBundle inside fxml

seb.
seb. Member Posts: 2
edited Mar 14, 2017 5:12AM in JavaFX 2.0 and Later

Hi, I got a question. I have a Resource class that extends java.util.ListResourceBundle, which holds several values. These are not only Strings but also doubles, for example. Now I want to access these values inside my FXML. It works very easy with String resources with the % sign, but did not work for me with any other type.

Here is some example code:

Resource class:

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Resources</span><span class="pln"> </span><span class="kwd">extends</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">util</span><span class="pun">.</span><span class="typ">ListResourceBundle</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br/><br/>   </span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">final</span><span class="pln"> </span><span class="typ">Object</span><span class="pun">[][]</span><span class="pln"> OBJECTS </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Object</span><span class="pun">[][]{</span><span class="pln"><br/>   </span><span class="pun">{</span><span class="str">"FOO"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"foo"</span><span class="pun">},</span><span class="pln"><br/>   </span><span class="pun">{</span><span class="str">"BAR"</span><span class="pun">,</span><span class="pln"> </span><span class="lit">123d</span><span class="pun">}</span><span class="pln"><br/>   </span><span class="pun">}</span><span class="pln"><br/><br/>   </span><span class="lit">@Override</span><span class="pln"><br/>   </span><span class="kwd">protected</span><span class="pln"> </span><span class="typ">Object</span><span class="pun">[][]</span><span class="pln"> getContents</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br/>   </span><span class="kwd">return</span><span class="pln"> OBJECTS</span><span class="pun">;</span><span class="pln"><br/>   </span><span class="pun">}</span><span class="pln"><br/></span><span class="pun"></span>

<span class="pun">}</span>

Loading the FXML in Application class:

<span class="typ">Resources</span><span class="pln"> resources </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Resources</span><span class="pun">();</span><span class="pln"><br/></span><span class="typ">FXMLLoader</span><span class="pln"> loader </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">FXMLLoader</span><span class="pun">(</span><span class="pln">getClass</span><span class="pun">().</span><span class="pln">getResource</span><span class="pun">(</span><span class="str">"/foo.fxml"</span><span class="pun">),</span><span class="pln"> resources</span><span class="pun">);</span><span class="pln"><br/></span><span class="typ">Parent</span><span class="pln"> root </span><span class="pun">=</span><span class="pln"> loader</span><span class="pun">.</span><span class="pln">load</span><span class="pun">();</span>

FXML:

<span class="pun"><</span><span class="typ">Label</span><span class="pln"> text</span><span class="pun">=</span><span class="str">"%FOO"</span><span class="pun">></span><span class="pln"><br/></span><span class="pun"><</span><span class="typ">Polygon</span><span class="pun">></span><span class="pln"><br/>   </span><span class="pun"><</span><span class="pln">points</span><span class="pun">></span><span class="pln"><br/>   </span><span class="pun"><</span><span class="typ">Double</span><span class="pln"> fx</span><span class="pun">:</span><span class="pln">value</span><span class="pun">=</span><span class="str">"0"</span><span class="pun">/></span><span class="pln"><br/>   </span><span class="pun"><</span><span class="typ">Double</span><span class="pln"> fx</span><span class="pun">:</span><span class="pln">value</span><span class="pun">=</span><span class="str">"0"</span><span class="pun">/></span><span class="pln"><br/>   </span><span class="pun"><</span><span class="typ">Double</span><span class="pln"> fx</span><span class="pun">:</span><span class="pln">value</span><span class="pun">=</span><span class="str">"%BAR"</span><span class="pun">/></span><span class="pln"><br/>   </span><span class="pun"><</span><span class="typ">Double</span><span class="pln"> fx</span><span class="pun">:</span><span class="pln">value</span><span class="pun">=</span><span class="str">"0"</span><span class="pun">/></span><span class="pln"><br/>   </span><span class="pun"></</span><span class="pln">points</span><span class="pun">></span><span class="pln"><br/></span><span class="pun"></</span><span class="typ">Polygon</span><span class="pun">></span>

The first line in FXML works perfectly fine, however the line with %BAR crashes with <span class="pln">javafx</span><span class="pun">.</span><span class="pln">fxml</span><span class="pun">.</span><span class="typ">LoadException</span><span class="pun"></span><span class="pln"></span>

I posted this question on SO two weeks ago but never got an answer. (java - JavaFX: Access in FXML to a non-String-Object from a ResourceBundle - Stack Overflow )

I hope you can help me.

Thanks a lot!

Best Answer

  • bouye-JavaNet
    bouye-JavaNet Member Posts: 394 Silver Badge
    edited Mar 13, 2017 12:19AM Answer ✓

    Good and interesting question, but as far as I know it is not feasible as the source code of FXMLLoader (here for example, line 418) shows that it invokes the getString() method of the resource bundle. Thus it can never get anything else than String instances out of a bundle. Thus what your ask for it is not really feasible as is unless you write you own code to load FXML content. FXMLLoader is not final and can be overriden but the resolvePrefixedValue() method in which getString() gets invoked is private so it's a no-go.

Answers

  • bouye-JavaNet
    bouye-JavaNet Member Posts: 394 Silver Badge
    edited Mar 13, 2017 12:19AM Answer ✓

    Good and interesting question, but as far as I know it is not feasible as the source code of FXMLLoader (here for example, line 418) shows that it invokes the getString() method of the resource bundle. Thus it can never get anything else than String instances out of a bundle. Thus what your ask for it is not really feasible as is unless you write you own code to load FXML content. FXMLLoader is not final and can be overriden but the resolvePrefixedValue() method in which getString() gets invoked is private so it's a no-go.

  • seb.
    seb. Member Posts: 2
    edited Mar 14, 2017 5:12AM

    @bouye-JavaNet

    Thanks for clarifying this. I never got the idea to check the FXMLLoader code myself. See the SO-Post to check what I did in the end. Thanks again for answering

This discussion has been closed.