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

后端 未结 4 444
孤城傲影
孤城傲影 2020-11-27 22:54

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

4条回答
  •  醉梦人生
    2020-11-27 23:42

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

提交回复
热议问题