Recall that Applets are Java programs that run in a web browser. To create an Applet, we inherit from the Applet class:
import java.applet.*;
public class MyApplet extends Applet
{
}
Previously, we drew pictures in the Applet window by adding a paint method:
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawRect(10,10, 100,50);
}
}
These programs were not interactive: once the picture was drawn, the program was done.
We now want to add the ability to interact with the applet. For the moment, we won't need the paint method because we won't be drawing directly on the Applet canvas. Instead, we will be creating GUI objects (e.g. textboxes and buttons) that will go on the Applet. How this is done is explained below.
We begin with a simple Applet that has a single TextBox. When the user enters characters and hits return, all the characters are converted to upper case.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MyApplet extends Applet implements ActionListener {
TextField inputLine = new TextField(15);
public MyApplet() {
add(inputLine);
inputLine.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
String s = inputLine.getText();
String sUp = s.toUpperCase();
inputLine.setText(sUp);
}
}
An event in an Applet is an object that represents some action such as
clicking the mouse
pressing a key on the keyboard
closing a window
pressing a button
etc
Objects (e.g. Buttons, TextFields, mouse, etc) are said to generate or fire events. Different types of objects can fire different kinds of events. For example,
Every object that can fire an event is called an event source. Each event source can have one or more listener objects which get registered with the event source.
A listener object has methods that specify what will happen when an event is sent to the listener. These methods are called event handlers. You the programmer, must define these event handlers.
Let's look at some examples below.
Using TextField objects in your applet is a 3-step process:
TextField inputLine = new TextField(15);
add(inputLine);
The Layout Manager determines where objects are placed in the Applet window. The default layout manager is called FlowLayout. Here, objects are placed left-to-right, top-to-bottom in the order in which they are added. Thus, if multiple objects are added, the order in which they are added determines where they will appear.
When the window is resized, the positions of the objects are changed to fit the window size.
You can use Visual Cafe to place Buttons and TextFields by dragging and dropping. In this case, Visual Cafe will actually write the code for creating and attaching the objects!
TextField and Button objects are event sources that can generate ActionEvents:
- A TextField generates an ActionEvent when the user enters text and presses return
- A Button generates an ActionEvent when the user presses the button.
ActionEvents are handled by ActionListeners. The programmer must write these.
To create an ActionListener you must have a class that implements the ActionListener interface.
An interface is a way to force a common behavior. When a class implements an interface, the class is required to contain methods specified by the interface. In the case of the ActionListener interface, we must define a method
public void actionPerformed(ActionEven e) {....}There are at least 4 ways of doing this:
Let the Applet itself implement the ActionListener interface so that actionPerformed is simply a method in the Applet. This is what is done in Example 1 above.
Define a "local" class, that is, a class within a class. Below we rewrite Example 1 using this method:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MyApplet extends Applet {
TextField inputLine = new TextField(15);
public MyApplet() {
add(inputLine);
inputLine.addActionListener(new MyActionListener());
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String s = inputLine.getText();
String sUp = s.toUpperCase();
inputLine.setText(sUp);
}
}
}
Create an entire separate class that implements ActionListener. This is the same as above except that the class is not local. There is no reason to do this.
Finally, we can create an in-line (un-named) class:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MyApplet extends Applet {
TextField inputLine = new TextField(15);
public MyApplet() {
add(inputLine);
inputLine.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
String s = inputLine.getText();
String sUp = s.toUpperCase();
inputLine.setText(sUp);
}
}
);
}
}