1

I'm working currently on java project that uses Arabic Language, I found difficulty in writing in Arabic as shown in the image:

enter image description here

  1. I wrote Arabic without any edit.
  2. I added a reverse() method, it worked good but the letters aren't attached to each other, they're separate.

    StringBuilder input = new StringBuilder();
    input.append(jTextField2.getText());
    input = input.reverse();
    jTextField1.setText(input.toString());
    
  3. I use site the flip the text, it didn't work as well.

  4. I use the same site, but with jLabel it worked.

other method I use, but didn't work:

  • Try Orientation jTextField1.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

  • Change the IDE encoding to URT-8 (I'm using Netbeans-JDK8).

Can anyone help me how to write & print Arabic in java correctly?

LaLa
  • 101
  • 11
  • 1
    when you say reverse? what exactly do you mean? – Eugene Aug 10 '18 at 12:24
  • @Eugene I edit the answer – LaLa Aug 10 '18 at 12:28
  • 1
    Possible duplicate of [How to set the orientation of JTextArea from right to left (inside JOptionPane)](https://stackoverflow.com/questions/6475320/how-to-set-the-orientation-of-jtextarea-from-right-to-left-inside-joptionpane) – Karol Dowbecki Aug 10 '18 at 12:28
  • Have you tried to set the locale? jTextField1.getInputContext().selectInputMethod(new Locale("ar", "LB")); – dirbacke Aug 10 '18 at 12:41
  • 2
    Nonetheless, a real [mcve] would be helpful. As in: putting a small class with a real main method that puts up that UI, and that people can simply run themselves to see your issue. – GhostCat Aug 10 '18 at 12:42
  • Please add to your question the exact sequence of Arabic characters (as text, not just as an image) that you are finding cannot be properly reversed. – VGR Aug 10 '18 at 16:36
  • @dirbacke yes, didn't work as well. – LaLa Aug 15 '18 at 22:40
  • @VGR I don't know if I understand u well, but this is what i'm aiming to get: (مرحبا بالعالم) – LaLa Aug 15 '18 at 22:44
  • Is that the reversed text? If it is, then what is the original text you are inputting before reversing it? – VGR Aug 16 '18 at 02:18

2 Answers2

1

Please refer to this question - Forcing RTL order in a JTextArea

Here is a sugestion to start the string with the character \u202e to force the text to be RTL.

Also i think it is not good approach to reverse the string, as it is not good user experience when the user do "copy paste", as he will copy reversed string...

Tamir Adler
  • 341
  • 2
  • 14
  • I added the code `jTextField1.getDocument().putProperty( TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);` but didn't work with. – LaLa Aug 15 '18 at 22:38
1

A string entirely composed of characters from the Arabic block should render with correct RTL presentation without any directionality control characters. If it does not, it is likely that you have a problem with your operating system configuration, not with your Java code. Reversing the string is a terrible idea. Trying for visual-order rendering is going to get all messed up.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • I added the code `jTextField1.getDocument().putProperty( TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL);` but didn't work with. – LaLa Aug 15 '18 at 22:38