Get and Set the Tab Size of a JTextArea in Java
Description
The following code shows how to get and Set the Tab Size of a JTextArea.
Example
import java.awt.Font;
// w w w .j a v a 2 s . com
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Main extends JFrame {
public Main() {
JTextArea textarea = new JTextArea("Initial Text");
textarea.setFont(new Font("LucidaSans", Font.PLAIN, 40));
add(textarea);
// Get the default tab size
int tabSize = textarea.getTabSize(); // 8
// Change the tab size
tabSize = 4;
textarea.setTabSize(tabSize);
Font font = textarea.getFont();
System.out.println(font);
}
public static void main(String[] args) {
JFrame frame = new Main();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »