How to get the Tab Size of a JTextArea in Java?

Java 8Object Oriented ProgrammingProgramming

To get the tab size from a JTextArea, you can use the getTabSize() method −

textArea.getTabSize();

We will assign it to int variable and display the size in the Console −

int size = textArea.getTabSize();
System.out.println("Tab Size = "+size);

The following is an example to set the tab size of a JTextArea −

Example

package my;
import java.awt.GridLayout;
import javax.swing.*;
public class SwingDemo {
   SwingDemo() {
      JFrame frame = new JFrame();
      JTextArea textArea = new JTextArea("This is demo text.");
      int size = textArea.getTabSize();
      System.out.println("Tab Size = "+size);
      frame.add(textArea);
      frame.setSize(550,300);
      frame.setLayout(new GridLayout(2, 2));
      frame.setVisible(true);
   }
   public static void main(String args[]) {
      new SwingDemo ();
   }
}

Output

The tab size would be visible in Console −

raja
Published on 06-Jun-2019 09:55:11
Advertisements