1

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?

Draque Thompson
  • 908
  • 9
  • 16
  • The caret is just the place where the next character will be entered, it has nothing to do with text rendering. Also, is there a reason why you want characters which are, by Unicode definition, not RTL, be displayed as RTL? – RealSkeptic Jun 01 '15 at 18:08
  • I am building a program being used by linguists, and they need to be able to create fonts representing new character sets and be able to type using the fonts RTL. Most that they have created so far just place new characters over existing letters. When you have characters that are not included in unicode and you need a solution that isn't so involved as getting them unicode approved, this seemed like the best solution. – Draque Thompson Jun 01 '15 at 18:14
  • What about mapping character sets that need to be "RTL" onto Arabic/Farsi/Hebrew letters? – RealSkeptic Jun 01 '15 at 18:54
  • That is a possibility, but some of the users simply wish to use typical characters in reverse order, as well. If that is the only way that can be done, I suppose that it how it has to be, but I can't help but think that there is a better way. If I could set the value that tells Java to render text RTL, I could just modify that. Do you happen to know of where that is? – Draque Thompson Jun 01 '15 at 19:36
  • There is no such "value". Java simply follows the Unicode algorithm in rendering the object. You can add RTL Override characters to the text (try displaying a text that starts with `\u202e`), but this would get very complicated if your users want to have mixed-direction text. – RealSkeptic Jun 01 '15 at 19:38
  • Actually, this will work perfectly, thank you! I am having to parse and dive into the text manually regardless, so just adding in the \u202e and \u202c characters will not be much more work. Thank you! – Draque Thompson Jun 01 '15 at 20:00

2 Answers2

1

What about component orientation?

boolean rtl = true;
t_text.setComponentOrientation(rtl ? 
    ComponentOrientation.RIGHT_TO_LEFT : 
    ComponentOrientation.LEFT_TO_RIGHT);

what you said about hello! and !olleh nothing like this will happen :) this is reverse not right-to-left. RTL of I am Soley! is !Soley am I which it shows words starting from right to left.

By the way, use a JTextPane instead of JTextArea, JTextArea I believe does not support RTL. That's what I read somewhere when I wanted to make JtextPane RTL once.

Well, if you want to reverse the input string, use:

public static String getReverse(String str) {
   return new StringBuffer(str).reverse().toString();
}

However, if you want to reverse all words, then split them after you reversed the whole input:

String[] list = getReverse("I am soley!").split(" ");
String[] ret = new String[list.length];
int len = list.length;
for(String w:list){
   ret[--len] = s;
}
list = null;
// you have your words reversed in ret array
Soley
  • 1,716
  • 1
  • 19
  • 33
  • That's The orientation is half of what I need, but "hello!" -> "!olleh" is the other half. Do you know any way that I could force Java to do that? – Draque Thompson Jun 01 '15 at 18:20
  • To reverse it while typing? – Soley Jun 01 '15 at 18:21
  • Yes, I am looking for it to be reversed while typing. Reversing the character order normally would be very simple, but I need to be able to have people type and input text like this. – Draque Thompson Jun 01 '15 at 18:41
  • You may use another label or textbox to view the reversed version of your input text, like what you see here, you type and then see the preview beneath it. – Soley Jun 02 '15 at 06:07
  • True, but that would force the user to look at the text backward as they were typing, then later see if flipped around. I've decided to go with the \u220e and \u202c characters to force RTL and LTR character ordering as is necessary. – Draque Thompson Jun 02 '15 at 20:50
0

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.

Draque Thompson
  • 908
  • 9
  • 16