Specify ResourceBundle within FXML file in JavaFX

3.6k Views Asked by At

One can set ResourceBundle in a Java class using code below.

FXMLLoader loader = new FXMLLoader(getClass().getResource("my_view.fxml"));
loader.setResources(MyRes.getBundle());    
//Node myNode = (Node) loader.load();

Is it possible to specify ResourceBundle in FXML file itself?

2

There are 2 best solutions below

0
On BEST ANSWER

From Introduction to FXML:

<fx:include> also supports attributes for specifying the name of the resource bundle that should be used to localize the included content, as well as the character set used to encode the source file.

<fx:include source="filename" resources="resource_file" charset="utf-8"/>

Not sure about the situation where it is not an included file as I didn't see the documentation on that (from some quick tests, I don't think it's possible, though I could be wrong).

2
On

Do you mean this?

<Label text="%user.firstname"/>
<Label text="%user.lastname"/>

You can use the % sign to add text from a resource file to a component in your .fxml file.

Considering that, your resource file must contain those two keys with values.