How to set the orientation of JTextArea from right to left (inside JOptionPane)

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);
            }
        });
    }
}

Leave a Comment