Arabic, being a right-to-left script, poses certain challenges when it comes to rendering in JavaFX applications. One common issue that developers encounter is Arabic text appearing with spaces between the characters, which disrupts the proper display of the text. In this article, we will explore a solution to resolve this problem and ensure correct rendering of Arabic text in JavaFX.
Example:
To illustrate the impact of these changes, let's consider an example where Arabic text is rendered incorrectly with spaces between the characters. Here is an image demonstrating the incorrect rendering:
After applying the suggested solution, the Arabic text will be rendered correctly. Here is an image showcasing the corrected rendering:
Solution:
To resolve Arabic text rendering issues in JavaFX, we can follow these steps:
1- Enable Arabic text shaping:
Arabic text requires special shaping to connect the letters and form the appropriate ligatures. By enabling Arabic text shaping in JavaFX, we can ensure that the characters are correctly connected and displayed. This can be done by setting the system property prism.text to t2k. You can achieve this programmatically at the beginning of your JavaFX application using the following code snippet:
System.setProperty("prism.text", "t2k");
2- Configure font rendering settings:
Font rendering can also impact the appearance of Arabic text. To improve the smoothness of text rendering, ensure that anti-aliasing is enabled. You can set the anti-aliasing property using the following code snippet:
System.setProperty("prism.lcdtext", "false");
System.setProperty("prism.text", "t2k");
Adjust the anti-aliasing property as needed based on your requirements.
It's important to note that these settings need to be applied before launching your JavaFX application.
Where to put these configurations
Remember to apply these settings before launching your JavaFX application to ensure the desired rendering behavior. With these changes in place, you can provide a better user experience for Arabic-speaking users and enhance the localization capabilities of your JavaFX applications.
public static void main(String[] args) {
// Fix Arabic letters in JavaFX
System.setProperty("prism.lcdtext", "false");
System.setProperty("prism.text", "t2k");
// Launch the JavaFX application
launch(args);
}
This is a full example of the Main Class
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
// Create a label
Label labelArabic = new Label("هذا نص عربي للتجربة");
labelArabic.setStyle("-fx-font-size: 70px;-fx-font-weight: bold;");
// Create a VBox as the root node
VBox vBox = new VBox(labelArabic);
vBox.setAlignment(Pos.CENTER);
vBox.setPadding(new Insets(50));
vBox.setSpacing(20);
// Create a scene and place it in the stage
primaryStage.setScene(new Scene(vBox));
// show the stage
primaryStage.show();
}
public static void main(String[] args) {
// Fix Arabic letters in JavaFX
System.setProperty("prism.lcdtext", "false");
System.setProperty("prism.text", "t2k");
// Launch the JavaFX application
launch(args);
}
}


Top comments (3)
Very Helpful Thank You
Thanks for ur help ❤️
Thank you Abdelrahman!
It’s clear you’ve put significant effort into isolating the root cause rather than just applying a quick CSS hack, which is the right approach when dealing with complex UI frameworks. For anyone building enterprise desktop tools in Arabic or other RTL languages, understanding the specific font fallback chains and system local detection is absolutely critical to avoid these subtle but jarring display issues.
Your breakdown of the debugging process provides a solid reference for others who might be hitting similar walls with non-Latin scripts in JavaFX. It’s a great reminder that while web standards are powerful, desktop environments still require a bit more manual orchestration of resources and fonts.
I’d love to hear if you encountered any specific challenges with font licensing or embedding when testing the solution, as that often becomes the next bottleneck once the rendering logic is fixed. Thanks for sharing this practical insight with the community.