Text areas | CodeGuru

Text areas

Bruce Eckel’s Thinking in Java Contents | Prev | Next A TextArea is like a TextField except that it can have multiple lines and has significantly more functionality. In addition to what you can do with a TextField, you can append text and insert or replace text at a given location. It seems like this […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 1, 2001
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

A


TextArea
is like a
TextField
except that it can have multiple lines and has significantly more
functionality. In addition to what you can do with a
TextField,
you can append text and insert or replace text at a given location. It seems
like this functionality could be useful for
TextField
as well, so it’s a little confusing to try to detect how the distinction
is made. You might think that if you want
TextArea
functionality everywhere you can simply use a one line
TextArea
in places where you would otherwise use a
TextField.
In Java 1.0
,
you also got scroll bars with a
TextArea
even
when they weren’t appropriate; that is, you got both vertical and
horizontal scroll bars for a one line
TextArea.
In Java 1.1

this was remedied with an extra constructor that allows you to select which
scroll bars (if any) are present. The following example shows only the Java 1.0

behavior, in which the scrollbars are always on. Later in the chapter
you’ll see an example that demonstrates Java 1.1
TextAreas.
//: TextArea1.java
// Using the text area control
import java.awt.*;
import java.applet.*;
 
public class TextArea1 extends Applet {
  Button b1 = new Button("Text Area 1");
  Button b2 = new Button("Text Area 2");
  Button b3 = new Button("Replace Text");
  Button b4 = new Button("Insert Text");
  TextArea t1 = new TextArea("t1", 1, 30);
  TextArea t2 = new TextArea("t2", 4, 30);
  public void init() {
    add(b1);
    add(t1);
    add(b2);
    add(t2);
    add(b3);
    add(b4);
  }
  public boolean action (Event evt, Object arg) {
    if(evt.target.equals(b1))
      getAppletContext().showStatus(t1.getText());
    else if(evt.target.equals(b2)) {
      t2.setText("Inserted by Button 2");
      t2.appendText(": " + t1.getText());
      getAppletContext().showStatus(t2.getText());
    }
    else if(evt.target.equals(b3)) {
      String s = " Replacement ";
      t2.replaceText(s, 3, 3 + s.length());
    }
    else if(evt.target.equals(b4))
      t2.insertText(" Inserted ", 10);
    // Let the base class handle it:
    else
      return super.action(evt, arg);
    return true; // We've handled it here
  }
} ///:~ 

There


are several different


TextArea

constructors, but the one shown here gives a starting string and the number of


rows and columns. The different buttons show getting, appending, replacing, and


inserting text.


Contents

|

Prev

|

Next
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.