How to create aText Area in Java

The text area component is similar to the text field component, except in that it allows for multiple lines of text. In Figure 8.1, the component below the text "Enter Name and Address" is an example of a text area component. To create a text area component, first declare the object to be of type JTextArea. For example,

JTextArea addressArea;



By default, the text area is set up without scroll bars. To add scroll bars, you create a JScrollPane object then add the text area to the JScrollPane object. So declare a JScrollPane object, for example

JScrollPane addressPane;

Then invoke the javax.swing.JTextArea constructor, for example

addressArea = new JTextArea(3,12);

where the constructor arguments specify the minimum number of rows and columns in the text area itself. Now add addressArea to a scrollpane using the JScrollPane constructor, as follows:

addressPane = new JScrollPane(addressArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
and JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS indicate that the vertical and horizontal scrollbars are always visible. The default is to show no scrollbars until the text area becomes full with text entered by the user.

No comments:

Post a Comment