2

I want to align the text in a TextArea to the right. I tried the following code:

     Form form = new Form();
     TextArea textArea = new TextArea("Some Arabic text ...");
     textArea.setRTL(true);
     textArea.setAlignment(RIGHT);
     form.addComponent(textArea);


The result was just moving the scroll to left,
But the text is still not aligned RIGHT,
check the image below:

enter image description here

So how to align the content to the RIGHT ?

vbence
  • 20,084
  • 9
  • 69
  • 118
Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82

3 Answers3

2

It may sound crazy for the first instance :) but setting the alignment to TextArea.LEFT solved the issue and now it's RIGHT aligned !

    Form form = new Form();
    TextArea textArea = new TextArea("Some Arabic text ...");
    textArea.setRTL(true);
    textArea.setAlignment(TextArea.LEFT);
    form.addComponent(textArea);

Setting it to LEFT makes the displayed text RIGHT aligned !

Or by removing the textArea.setRTL(true) which is mirroring the display

    Form form = new Form();
    TextArea textArea = new TextArea("Some Arabic text ...");
    textArea.setAlignment(TextArea.RIGHT);
    form.addComponent(textArea);



For those who are interested in more complicated details when it's set to RTL:
the paint method of TextArea class is

public void paint(Graphics g) {
    UIManager.getInstance().getLookAndFeel().drawTextArea(g, this);
}

And drawTextArea method in DefaultLookAndFeel is as follows:

int align = ta.getAbsoluteAlignment();
// remaining code is here in initial source
switch(align) {
     case Component.RIGHT:
          x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText);
          break;
     // remaining code is here in initial source
}
g.drawString(displayText, x, y);

Unfortunately TextArea.RIGHT value is 3
But when calling ta.getAbsoluteAlignment() it returns 1 (despite that the object's alignment is set by code to TextArea.RIGHT !!)
Meanwhile TextArea.Left value is 1
That's why it matched the value in the switch and was aligned to RIGHT

BTW, if you set

textArea.setAlignment(Component.RIGHT); 

it will also be wrong, because Component.RIGHT outside the paint method has the value 3 not 1 !

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
  • 2
    As you are activating **RTL** for your `TextArea` the directions of the text rendering is reversed hence Left is considered as Right. I would suggest you to get clarification on this from Shai by filling a post @ [LWUIT Java Form](http://www.java.net/forums/mobile-embedded/lwuit). And thus contribute to the LWUIT community. – Vimal Nov 30 '11 at 19:26
  • 1
    Vimal is correct. It sounds odd but it makes sense once you think about it. When you activate bidi you are effectively reversing the display by default so right becomes left and east becomes west... That makes sense in RTL applications since in those applications 99% of the UI components would want to do the exact opposite of the English version, hence you can write an English UI and localize it to bidi by just translating and flipping the bidi switch. – Shai Almog Dec 01 '11 at 06:31
  • Yup, it can be done just by removing this line of code in my initial post: **textArea.setRTL(true);** and the remainig as it is. Thanks for this valuable comment ! – Ashraf Bashir Dec 01 '11 at 13:30
  • BTW, I think RIGHT and LEFT shouldn't being reversed, this is very ambiguous in terminology, especially with those who come from other APIs backgrounds. Instead of this, I think the RTL flag should auto set the alignment layout to RIGHT unless the code specifies otherwise, this will avoid mis-using the terms. Left should remain left as its name same for right. Thanks so much for the clarification – Ashraf Bashir Dec 01 '11 at 13:39
1

You only have to write 'TextArea.RIGHT' instead of 'RIGHT'

textArea.setAlignment(TextArea.RIGHT);
frayab
  • 2,512
  • 20
  • 25
0

You can use the following line:

TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
Worthwelle
  • 1,244
  • 1
  • 16
  • 19
Amr
  • 1