Search This Blog

ADVANCED JAVA

C-PROGRAMMING

Wednesday, December 28, 2022

Java AWT TextArea

 

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

  1. public class TextArea extends TextComponent  

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.ConstructorDescription
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 nameDescription
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

  1. //importing AWT class  
  2. import java.awt.*;    
  3. public class TextAreaExample    
  4. {    
  5. // constructor to initialize  
  6.      TextAreaExample() {    
  7. // creating a frame  
  8.         Frame f = new Frame();    
  9. // creating a text area  
  10.             TextArea area = new TextArea("Welcome to javatpoint");    
  11. // setting location of text area in frame  
  12.         area.setBounds(1030300300);    
  13. // adding text area to frame  
  14.         f.add(area);  
  15. // setting size, layout and visibility of frame    
  16.         f.setSize(400400);    
  17.         f.setLayout(null);    
  18.         f.setVisible(true);    
  19.      }    
  20. // main method  
  21. public static void main(String args[])    
  22. {    
  23.    new TextAreaExample();    
  24. }    
  25. }    

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

  1. // importing necessary libraries  
  2. import java.awt.*;    
  3. import java.awt.event.*;    
  4. // our class extends the Frame class to inherit its properties  
  5. // and implements ActionListener interface to override its methods  
  6. public class TextAreaExample2 extends Frame implements ActionListener {    
  7. // creating objects of Label, TextArea and Button class.  
  8. Label l1, l2;    
  9. TextArea area;    
  10. Button b;    
  11. // constructor to instantiate  
  12. TextAreaExample2() {    
  13. // instantiating and setting the location of components on the frame  
  14.     l1 = new Label();    
  15.     l1.setBounds(505010030);    
  16.     l2 = new Label();    
  17.     l2.setBounds(1605010030);    
  18.     area = new TextArea();    
  19.     area.setBounds(20100300300);    
  20.     b = new Button("Count Words");    
  21.     b.setBounds(10040010030);    
  22.   
  23. // adding ActionListener to button  
  24.     b.addActionListener(this);    
  25.   
  26. // adding components to frame  
  27.     add(l1);  
  28. add(l2);  
  29. add(area);  
  30. add(b);    
  31. // setting the size, layout and visibility of frame  
  32.     setSize(400450);    
  33.     setLayout(null);    
  34.     setVisible(true);    
  35. }    
  36. // generating event text area to count number of words and characters  
  37. public void actionPerformed(ActionEvent e) {    
  38.     String text = area.getText();    
  39.     String words[]=text.split("\\s");    
  40.     l1.setText("Words: "+words.length);    
  41.     l2.setText("Characters: "+text.length());    
  42. }    
  43. // main method  
  44. public static void main(String[] args) {    
  45.     new TextAreaExample2();    
  46. }    
  47. }    

Output:

java awt textarea example 2

0 comments:

Post a Comment

OS

Top