Java Reference
In-Depth Information
The following code from the TextComponentDemo, available on the Web
site for this topic, demonstrates using the TextField and TextArea components.
Study the program, determining how the GUI looks and what the program
does.
textField = new TextField();
textArea = new TextArea(“”, 0, 0,
TextArea.SCROLLBARS_VERTICAL_ONLY);
textArea.setEditable(false);
//setup the event handling
CreateList listener = new CreateList(textField, textArea);
textField.addActionListener(listener);
The event handling is done using the following CreateList class, and I want
you to pay special attention to its actionPerformed() method.
import java.awt.*;
import java.awt.event.*;
public class CreateList implements ActionListener
{
private int counter;
private TextField source;
private TextArea destination;
public CreateList(TextField s, TextArea d)
{
source = s;
destination = d;
}
public void actionPerformed(ActionEvent e)
{
Object component = e.getSource();
String action = e.getActionCommand();
if(component instanceof TextField || action.equals(“Enter”))
{
String text = source.getText();
counter++;
destination.append(counter + “. “ + text + “\n”);
source.setText(“”);
}
else if(action.equals(“Clear”))
{
destination.setText(“”);
counter = 0;
}
}
}
Search WWH ::




Custom Search