Thursday 14 May 2015

TextArea in Java

Text areas are designed to hold large chunks of text, where large is more than one line. TextArea extends TextComponent by adding a number of additional methods as well as automatic scrolling of the text. Listing 17.24 shows examples of the different TextArea creator methods displayed in Figure 17.32.

Listing 17.24. TextArea creator methods.
import java.awt.*;
import java.applet.Applet;


public class text_areas extends Applet
{
    TextArea text_space[];
    Label labels[];
    int n_fields;

    public void init()
    {

        int i;
        String sample_text; 

        n_fields = 4; 
        labels = new Label[n_fields]; 
        text_space = new TextArea[n_fields];
        //This string could have included \n to force returns
        sample_text = "This is a very long piece of text which is" +
        " designed to show how " +
        "a text area can hold a lot of text without you having to do a lot of" +
              "work." +
        "In general text areas are good for holding large chunks of text or for" +
         "getting " +
        "long answers back from the user.  TextAreas have lots of methods for " +
        "manipulating the their contents and for controlling how the text is " +
        "scrolled"; 

        //define the TextAreas and their labels.
        labels[0] = new Label("default text area");
        text_space[0] = new TextArea();
        labels[1] = new Label("5 by 30 text area");
        text_space[1] = new TextArea(5,30);
        labels[2] = new Label("Filled with a sample string");
        text_space[2] = new TextArea(sample_text);
        labels[3] = new Label("8 by 50 text area with a sample string");
        text_space[3] = new TextArea(sample_text,8,50);
        //add everything to the applet's panel
        for (i=0;i<n_fields;i++){ 
            add(labels[i]); 
            add(text_space[i]); 
        }

    }

}

TextArea extends TextComponent in several ways, but one of the most useful ones is its support of more powerful text-manipulation methods, such as insert and append. The most useful TextArea methods follow.
new TextArea()
Defines a default empty TextArea.
new TextArea(int rows, int columns)
Defines an empty TextArea with the specified number of rows and columns.
new TextArea(String the_contents)
Defines a TextArea that contains the specified string.
new TextArea(String the_contents, int rows, int columns)
Defines a TextArea containing the specified string and with a set number of rows and columns.
appendText(String new_text)
Appends the specified string to the current contents of the TextArea.
int, getColumns()
Returns the current width of the TextArea in columns.
int, getRows()
Returns the current number of rows in a TextArea.
insertText(String the_text, int where_to_add)
Inserts the specified string at the specified location.
replaceText(String new_text, int start, int stop)
Takes the text between start and stop, inclusive, and replaces it with the specified string.  
  

No comments:

Post a Comment