Search code examples
javatextbidirectional

Forcing RTL order in a JTextArea


I am trying to force a JTextArea (and other text components through my application) to allow users to type RTL. This works correctly for input such as Arabic, but I must also be able to set it to apply this to standard text. Therefore, if I type "hello!" it will show up as "!olleh".

I have tried using the applyOrientation() method and setting the text area to have RTL like so:

jTextPane1.getDocument().putProperty(
        TextAttribute.RUN_DIRECTION,
        TextAttribute.RUN_DIRECTION_RTL);

I have had no success thus far. It has been suggested that I try the Bidi libraries, but these seem focused on interpretation of text which is already bidirectional. When I apply the above, I get right justified text, but it remains LTR in terms of the character order. Is there anything that I'm missing here? Is there a way to set the value that Java itself checks when looking for which direction text entry should move the carat?


Solution

  • My problem was solved by the \u202e and the \u202c characters pointed out by RealSkeptic. One will force all characters following it into RTL form, the other forces all characters following it into LTR form. This is a quick and dirty fix, but for my problem, it offered the solution I needed.