Home » Java AWT TextArea

Java AWT TextArea

by Online Tutorials Library

Java AWT TextArea

The object of a TextArea class is a multiline region that displays text. It allows the editing of multiple line text. It inherits TextComponent class.

The text area allows us to type as much text as we want. When the text in the text area becomes larger than the viewable area, the scroll bar appears automatically which helps us to scroll the text up and down, or right and left.

AWT TextArea Class Declaration

Fields of TextArea Class

The fields of java.awt.TextArea class are as follows:

  • static int SCROLLBARS_BOTH – It creates and displays both horizontal and vertical scrollbars.
  • static int SCROLLBARS_HORIZONTAL_ONLY – It creates and displays only the horizontal scrollbar.
  • static int SCROLLBARS_VERTICAL_ONLY – It creates and displays only the vertical scrollbar.
  • static int SCROLLBARS_NONE – It doesn’t create or display any scrollbar in the text area.

Class constructors:

Sr. no. Constructor Description
1. TextArea() It constructs a new and empty text area with no text in it.
2. TextArea (int row, int column) It constructs a new text area with specified number of rows and columns and empty string as text.
3. TextArea (String text) It constructs a new text area and displays the specified text in it.
4. TextArea (String text, int row, int column) It constructs a new text area with the specified text in the text area and specified number of rows and columns.
5. TextArea (String text, int row, int column, int scrollbars) It construcst a new text area with specified text in text area and specified number of rows and columns and visibility.

Methods Inherited

The methods of TextArea class are inherited from following classes:

  • java.awt.TextComponent
  • java.awt.Component
  • java.lang.Object

TetArea Class Methods

Sr. no. Method name Description
1. void addNotify() It creates a peer of text area.
2. void append(String str) It appends the specified text to the current text of text area.
3. AccessibleContext getAccessibleContext() It returns the accessible context related to the text area
4. int getColumns() It returns the number of columns of text area.
5. Dimension getMinimumSize() It determines the minimum size of a text area.
6. Dimension getMinimumSize(int rows, int columns) It determines the minimum size of a text area with the given number of rows and columns.
7. Dimension getPreferredSize() It determines the preferred size of a text area.
8. Dimension preferredSize(int rows, int columns) It determines the preferred size of a text area with given number of rows and columns.
9. int getRows() It returns the number of rows of text area.
10. int getScrollbarVisibility() It returns an enumerated value that indicates which scroll bars the text area uses.
11. void insert(String str, int pos) It inserts the specified text at the specified position in this text area.
12. protected String paramString() It returns a string representing the state of this TextArea.
13. void replaceRange(String str, int start, int end) It replaces text between the indicated start and end positions with the specified replacement text.
14. void setColumns(int columns) It sets the number of columns for this text area.
15. void setRows(int rows) It sets the number of rows for this text area.

Java AWT TextArea Example

The below example illustrates the simple implementation of TextArea where we are creating a text area using the constructor TextArea(String text) and adding it to the frame.

TextAreaExample .java

Output:

java awt textarea example 1

Java AWT TextArea Example with ActionListener

The following example displays a text area in the frame where it extends the Frame class and implements ActionListener interface. Using ActionListener the event is generated on the button press, where we are counting the number of character and words entered in the text area.

TextAreaExample2.java

Output:

java awt textarea example 2


Next TopicJava AWT CheckBox

You may also like