Using Unicode characters with JavaFX
Asked Answered
B

1

7

I've been playing around with swing for some time and decided to check out FX now. So far I'm finding it a lot easier and more fun to work with as compared to swing but I've run into a small speed bump and after hours of looking around I just can't find a solution.

I am unable to use \u when I try to add it through the fxml file It works fine if I don't use the fxml but I want to use the scene builder as it is more convenient. Here's the small piece of code:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.131" fx:controller="baitform.designDocController">
    <children>
        <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
        <Label layoutX="145.0" layoutY="129.0" text="\u0644\u0627\u062B\u0627\u0646\u0649" />
    </children>
</AnchorPane>

The error I keep getting is

Caused by: javafx.fxml.LoadException: Invalid escape sequence.

Not sure if relevant, but I'm using jdk1.8.0_131 & netbeans 8.2

If anyone could point me in the right direction here I'd really appreciate it.

Brenner answered 27/2, 2018 at 19:7 Comment(0)
T
12

FXML is an XML, and so you need to use XML escaping:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.131" fx:controller="baitform.designDocController">
    <children>
        <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
        <Label layoutX="145.0" layoutY="129.0" text="&#x0644;&#x0627;&#x062B;&#x0627;&#x0646;&#x0649;" />
    </children>
</AnchorPane>

That being said, if you are able to input the characters, you can just insert them as is.

See also: https://www.w3.org/International/questions/qa-escapes

Tricia answered 27/2, 2018 at 19:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.