How can we set the orientation of a JTextArea from right to left in Java?


A JTextArea is a subclass of JTextComponent class and it is a multi-line text component to display the text or allow a user to enter the text. A JTextArea can generate a CaretListener interface when we are trying to implement the functionality of the JTextArea. By default, a JTextarea allows the orientation from left to right, if the user wants to enter a text from right to left by using the setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFTmethod of JTextArea class.

Example

import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
public class JTextAreaOrientationTest extends JFrame {
   private JTextArea textArea;
   public JTextAreaOrientationTest() {
      setTitle("JTextAreaOrientation Test");
      textArea = new JTextArea();
      textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
      add(new JScrollPane(textArea), BorderLayout.CENTER);
      setSize(400, 275);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String args[]) {
      new JTextAreaOrientationTest();
   }
}

Output

Updated on: 10-Feb-2020

766 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements