12

I have JScrollPane with JTextArea inside it and I am trying to set the JTextArea's orientation from right to left so the text inside it will start from the right and the scrollbar will be on the left

I've tried the following but they didn't affect the direction of the orientation:

txt.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setAlignmentX(JTextArea.RIGHT_ALIGNMENT);

EDIT:

the two answers camickr & trashgod provided work fine but not in my program where I use my JTextArea as an object Message and pass it to OptionPane.

EDIT2:

I figured out that setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); doesn't work if I apply it on the JOptionPane contents .. is there an alternative solution to this issue?

Similar to my code:

import java.awt.*;
import java.util.*;
import javax.swing.*;
public class TextArea extends JPanel
{
    private JTextArea txt = new JTextArea();
    public TextArea()
    {
        setLayout(new GridLayout());
        txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        JScrollPane scroll = new JScrollPane(txt);
        scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        setPreferredSize(new Dimension(200,200));
        this.add(scroll);
    }
    private void display()
    {
        Object[] options = {this};
        JOptionPane pane = new JOptionPane();
        int option = pane.showOptionDialog(null, null, "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
    }
    public static void main(String[] args)
    {
        new TextArea().display();
    }
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417

4 Answers4

11

and the scrollbar will be on the left

scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

so the text inside it will start from the right

textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

The text starts on the right side, but still gets append to the end as you type instead of being inserted at the beginning of the line.

Update:

I don't know why it doesn't work in an option pane. Here is a simple solution:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

public class Test
{
    public static void main(String args[]) throws Exception
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JTextArea textArea = new JTextArea(4, 20);
                JScrollPane scrollPane = new JScrollPane( textArea );
                JPanel panel = new JPanel();
                panel.add( scrollPane );

                scrollPane.addAncestorListener( new AncestorListener()
                {
                    public void ancestorAdded(AncestorEvent e)
                    {
                        JScrollPane scrollPane = (JScrollPane)e.getComponent();
                        scrollPane.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                    }

                    public void ancestorMoved(AncestorEvent e) {}
                    public void ancestorRemoved(AncestorEvent e) {}
                });

                JOptionPane.showMessageDialog(null, panel);
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
7

This seems to work.

enter image description here

import java.awt.ComponentOrientation;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/** @see http://stackoverflow.com/questions/6475320 */
public class RTLTextArea extends JPanel {

    private static final String s = "مرحبا العالم";
    private JTextArea jta = new JTextArea(7, 5);
    private Locale arabic = new Locale("ar", "KW");
    private ComponentOrientation arabicOrientation =
        ComponentOrientation.getOrientation(arabic);

    public RTLTextArea() {
        this.setLayout(new GridLayout());
        this.add(new JScrollPane(jta));
        this.applyComponentOrientation(arabicOrientation);
        for (int i = 0; i < 8; i++) {
            jta.append(s + "\n");
        }
    }

    private void display() {
        JFrame f = new JFrame("RTLTextAre");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new RTLTextArea().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • This works very well, but I don't know why it doesn't work in my program :/ I have to re-check my code again – Eng.Fouad Jun 25 '11 at 02:48
  • And that is why you where asked to post your SSCCE! Don't expect us to write the code, I already told you it worked fine. I don't believe in spoon feeding code when all you have to do is try a single line of code. You don't learn anything that way. – camickr Jun 25 '11 at 02:57
  • Updated example to use `applyComponentOrientation()`. – trashgod Jun 25 '11 at 03:14
2

this line

setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT)

change the correct order of the words.

i have this result

KBytes 80.78

0

The following lines solved my problem:

jTextArea1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
jTextArea1.setText(<text>);

They serve to:

  1. setComponentOrientation() changes the orientation of the TextArea; and,
  2. setText() refreshes TextArea immediately so it displays properly

Simply setting ComponentOrientation to RIGHT_TO_LEFT is not sufficient by itself. repaint() doesn't force the text to realign itself. A quick solution for me was to update the contents of the TextArea. That forced the text to realign itself.

Eric D
  • 1,363
  • 13
  • 9