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

后端 未结 4 453
孤城傲影
孤城傲影 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:30

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

提交回复
热议问题