This tip covers the basic technique for creating a standard text area that has the following features
- The ability to specify a height and width for the text area
- A vertical scroll bar that is always visible
- Word wrap within the text area
The complete code for this example is linked from the bottom of this page. The lines of code below show the basic steps for creating the text area.
The key lines are described as follows:
- Line 7 declares the text area
- Line 17 creates a new text area with no default text, a height of 5 lines and a width of 50 characters.
- Line 18 turns on word wrap for the text area. This prevents the text area from scrolling left and right.
- Line 19 adds text area to text area to a scroll pane, thus enabling scrolling.
- Line 20 is key. Without this statement, the scroll bar will not appear until there is enough text to require scrolling. Very ugly! By specifying JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, the scroll bar will always be visible irrespective of the text contained in the text area.