The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.text  [49 examples] > JTextArea  [7 examples]

e985. 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 e993 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();

 Related Examples
e982. Creating a JTextArea Component
e983. Modifying Text in a JTextArea Component
e984. Enumerating the Lines in a JTextArea Component
e986. Moving the Focus with the TAB Key in a JTextArea Component
e987. Enabling Word-Wrapping and Line-Wrapping in a JTextArea Component
e988. Implementing a Console Window with a JTextArea Component

See also: Actions and Key Bindings    Caret and Selection    Events    JEditorPane    JFormattedTextField    JTextComponent    JTextField    JTextPane    Styles   


© 2002 Addison-Wesley.