Text Watcher Applet
// this applet sets up two TextAreas where the user can type text.
// when the user types a character in either TextArea the character
// is copied into the other TextArea
import java.awt.*;
import java.awt.event.*;
public class TextWatcher11 extends java.applet.Applet
implements KeyListener
{
private TextArea top;
private TextArea bottom;
private Panel topPanel;
private Panel bottomPanel;
// create two Panels and the two text areas, add the text
// areas to the Panels and add the Panels to the applet
// the TextAreas are placed inside Panels so that their
// size can be specified
public void init()
{
setLayout( new GridLayout(2,1) );
topPanel = new Panel();
bottomPanel = new Panel();
topPanel.add( top = new TextArea(5,40) );
bottomPanel.add( bottom = new TextArea(5,40) );
add(topPanel);
add(bottomPanel);
top.addKeyListener(this);
bottom.addKeyListener(this);
}
// this method will receive an event when the user presses a key
// it checks which TextArea generated the event and copy the
// character typed to the other TextArea.
// the character typed is converted to a String so that it can be
// appended to the text in the TextArea
public void keyPressed(KeyEvent e)
{
if (e.getComponent() == top)
bottom.appendText((new Character(e.getKeyChar())).toString());
if (e.getComponent() == bottom)
top.appendText((new Character(e.getKeyChar())).toString());
}
// empty methods to satisfy the KeyListener interface
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
}