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


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 −

Updated on: 30-Jul-2019

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements