Applets - Part 4 -
Text Fields

bullet

In this example, we'll define and instantiate a textField object and several label objects. 

import java.applet.*;
import java.awt.*;

public class ShowSelectedText extends Applet
{
    private TextField blankTxt = new TextField(10);

    //  the text field will be 10 characters wide

    private Label message1 = new Label("Selected Text is: ");
    private Label message2 = new Label("------------------");

bullet

We'll use init and the default layout manager (Form) to place these objects on the display.

    public void init()
    {
        add(blankTxt);
        add(message1);
        add(message2);
        super.init();

        // use the parent constructor for all other initialization
    }

bullet

Finally, once someone has keyed in some text, any text that is highlighted (ie. selected) will be displayed in the message2 label thanks to the keyUp event

    public boolean keyUp(Event e, int keyValue)
    {
        message2.setText(blankTxt.getSelectedText());
        return super.keyUp(e, keyValue);

        // pass the event onto other event handlers that might "want" it
    }
}  /* end of applet */

bullet

Note, if you try this applet out, remember to change the applet name to "ShowSelectedText.class" in the .htm file with the code parameter -
<APPLET CODE="ShowSelectedText.class" ...

wpe5.jpg (12895 bytes)

Created & Maintained by DrB - Comments welcome.
Last edited on Friday, July 30, 2004