Issue
I want this part of my code with different languge so I am using bundles. I am trying to set the value of text in fxml file with "%" sign to have the value in the bundle.properties file.
I have the following code in the fxml file using Scenebuilder:
<?xml version="1.0" encoding="UTF-8"?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="iconBox" alignment="CENTER" maxHeight="-Infinity" maxWidth="-
Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="80.0"
prefWidth="80.0" xmlns="http://javafx.com/javafx/18"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.sarf.main.HomeIconsController">
<children>
<FontAwesomeIcon fx:id="iconName" glyphName="STAR" size="26" />
<Label fx:id="iconLabel" text="%anyThing">
<opaqueInsets>
<Insets />
</opaqueInsets>
<VBox.margin>
<Insets top="20.0" />
</VBox.margin>
</Label>
</children>
</VBox>
I am using properties file and trying to set the value of the text from the Controller of the fxml file. here is the code in the controller:
package com.sarf.main;
import com.sarf.main.models.HomeIcon;
import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
//import javafx.scene.layout.VBox;
public class HomeIconsController implements Initializable{
//@FXML
//private VBox iconBox;
@FXML
private FontAwesomeIcon iconName;
@FXML
private Label iconLabel;
@Override
public void initialize(URL url, ResourceBundle rb) {
iconLabel.setText("%grocery");
}
}
the main.java file:
package com.sarf.main;
import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.sql.*;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Locale local = new Locale("en_UK");
ResourceBundle bundle =
ResourceBundle.getBundle("com.sarf.main.resources.bundle", local);
Parent rootPane =
FXMLLoader.load(getClass().getResource("fxml/home_icons.fxml"), bundle);
Scene scene = new Scene(rootPane);
primaryStage.setTitle("SARF");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
the resources file is simple. I am just at the beginning of the project. but i faced this porblem. anyway the resources file for now includes one entry:
grocery= Market
when I run the code, the label text is shown like : "%grocery", % sign is interpreted as plain text and the value is not shown as it is in the bundle.properties file.
any help?
Solution
Resource resolution is performed by the FXMLLoader
when the FXML is parsed. So
<Label fx:id="iconLabel" text="%grocery" />
will result in the text of the label being set to the result of calling getString("grocery")
on the resource bundle.
Your code sets the text twice, first in the FXML file, but then again in the initialize()
method. The initialize()
method is invoked after the text has been set by parsing the FXML file, and of course just sets the text literally to the value provided. So in your code the label's text is initially set to "Market"
, but then immediately reset to "%grocery"
.
Remove the line
iconLabel.setText("%grocery");
from the initialize()
method and you will see the result you intended.
If you really want to reference the resource bundle in the controller, you can do so as it is passed to the initialize()
method. If you remove text="%grocery"
from the FXML file, you can do
iconLabel.setText(rb.getString("grocery"));
in the initialize()
method (instead of the line you currently have) and that will work too.
Related: see Accessing localisation ResourceBundle inside JavaFX FXML Controller for more information about accessing the resource bundle in the controller.
Answered By - James_D
Answer Checked By - David Marino (JavaFixing Volunteer)