Setting the Tab Size of a JTextArea Component
When a tab character (\t) is inserted into a text area, it is
represented by a space that is wide enough to reach the next tab stop.
The space expands or contracts, if the text preceding it is modified.
A tab space cannot be zero-width or wider than a tab stop.
By default, each tab stop is the same width and appears at
regular intervals. The tab stop width is controlled by the \meta{tab
size}. The tab size specifies a number of characters. The tab width
is this value multiplied by the maximal character width in the current
font. For example, if the widest character in the current font is 20
pixels and the tab size is 8, the tab width is 160 pixels; tab stops
will appear every 160 pixels in the text area.
All tab stops in a JTextArea are left-aligned. For other
types of tab stops, a JTextPane must be used; see
Customizing Tab Stops in a JTextPane Component.
// Create a text area
JTextArea textarea = new JTextArea();
// Get the default tab size
int tabSize = textarea.getTabSize(); // 8
// Change the tab size
tabSize = 4;
textarea.setTabSize(tabSize);
Font font = textarea.getFont();
Post a comment