Text fields | CodeGuru

Text fields

Bruce Eckel’s Thinking in Java Contents | Prev | Next A TextField is a one line area that allows the user to enter and edit text. TextField is inherited from TextComponent, which lets you select text, get the selected text as a String, get or set the text, and set whether the TextField is editable, […]

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


TextField
is a one line area that allows the user to enter and edit text.
TextField
is
inherited from
TextComponent,
which
lets you select text, get the selected text as a
String,
get or set the text, and set whether the
TextField
is editable, along with other associated methods that you can find in your
online reference. The following example demonstrates some of the functionality
of a
TextField;
you can see that the method names are fairly obvious:
//: TextField1.java
// Using the text field control
import java.awt.*;
import java.applet.*;
 
public class TextField1 extends Applet {
  Button
    b1 = new Button("Get Text"),
    b2 = new Button("Set Text");
  TextField
    t = new TextField("Starting text", 30);
  String s = new String();
  public void init() {
    add(b1);
    add(b2);
    add(t);
  }
  public boolean action (Event evt, Object arg) {
    if(evt.target.equals(b1)) {
      getAppletContext().showStatus(t.getText());
      s = t.getSelectedText();
      if(s.length() == 0) s = t.getText();
      t.setEditable(true);
    }
    else if(evt.target.equals(b2)) {
      t.setText("Inserted by Button 2: " + s);
      t.setEditable(false);
    }
    // Let the base class handle it:
    else
      return super.action(evt, arg);
    return true; // We've handled it here
  }
} ///:~ 

There


are several ways to construct a


TextField

;


the one shown here provides an initial string and sets the size of the field in


characters.

Pressing


button 1 either gets the text you’ve selected with the mouse or it gets


all the text in the field and places the result in


String
s

.


It also allows the field to be edited. Pressing button 2 puts a message and


s

into the text field and prevents the field from being edited (although you can


still select the text). The editability of the text is controlled by passing

setEditable( )
a
true
or
false.
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.