Java TextArea Multiple Lines Text

Java TextArea Multiple Lines Text

super("Practicing Text Area");

The above statement does the same purpose of setTitle(). It is another style of giving the title to the frame. We know earlier, super() statement must be the first one in the constructor if exists.

typeText = new TextArea("Type Here", 10, 20);

The TextArea object typeText is created to display 10 rows and 20 columns with a string "Type Here" that is displayed by default. If the string is omitted in the constructor, an empty text area is obtained.

displayText = new TextArea();
displayText.setRows(10);
displayText.setColumns(20);

The TextArea object displayText is created in a different way. An empty constructor is used and the properties like rows and columns are set separately with setRows() and setColumns() methods.

In contrast to a default constructor, the parameterized constructor gives the object, by default, properties at the time of creation itself. This style of creating object is easier than using a default constructor. The above set() methods can be used advantageously to change, dynamically, the size of the text area etc.

typeText.addTextListener(this);

As usual, the component typeText is registered with TextListener with addTextListener() method of TextArea. The "this" parameter represents an object of TextListener.

add(typeText, BorderLayout.NORTH);
add(displayText, BorderLayout.SOUTH);

typeText and displayText objects are added to frame, set with BorderLayout, in a different way by using the final static variables NORTH and SOUTH instead of general "North" and "South".

String str = typeText.getText();
displayText.setText(str);

With getText() method, the existing data in the text area typeText is obtained and is set (placed) in the text area displayText with method setText() mehod.

TextArea includes three methods to place the text. They are

  1. void append(String str) : Appends the string str to the existing data in the text area.
  2. void replaceRange(String str, int start, int end) : Replaces the existing text between the start and end positions with string str.
  3. void setText(String str) : Displays the text str in the text area. Inherited from its super class TextComponent. String str overrides all the earlier data. That is, the earlier data is lost. To retain earlier data, use append(String).
    method.

One more method appendText(String) also exists which differs from append() method in that appendText() is synchronized; but for both are same. The design pattern and internals of handling events is discussed figuratively in Event Handling.

4 thoughts on “Java TextArea Multiple Lines Text”

  1. SangeetaVaidya

    Respected Sir,

    Am a working parent of 3 kids so in class tuition is not possible.

    Do you teach online via Skype or GotoMeeting ?

    OR

    Can we BUY CD/DVD of ALL your classes ?

    If no then is there any institute in Hyderabad that does ONLINE JAVA coaching?

    Please help.

    Thanks

    Sangeeta Vaidya

Leave a Comment

Your email address will not be published.