Java Applets and AWT: Building Interactive Web Content
Applets
The Applet class provides everything needed to create applets. It is contained in the java.applet package. Therefore, if we want to develop an applet, we must import this package, as well as the java.awt package and javax.swing, which contain all the necessary tools to develop user interfaces. An applet is a program that runs through an Internet browser or the SDK tool appletviewer.exe.
Life Cycle of an Applet
An applet inherits its properties and class methods from the Applet class in the java.awt package. The java.applet library has several methods that the user can override to provide the required functionality. These methods follow a life cycle that is explained in the diagram below:
- init(): The first method to load when the applet is running, and it runs only once. It should be used to initialize variables.
- start(): Runs after init(). Enables restarting an applet after it has stopped. The start() method is executed every time the screen shows the HTML document containing the applet.
- paint(): Is executed every time you must repaint the output of an applet.
- stop(): Is executed when the browser leaves the HTML page containing the applet. Used to stop the applet; to restart it, the start() method is called.
- destroy(): Runs when the environment determines that the applet must be completely erased from memory. The stop() method always executes before destroy().
Passing Parameters to an Applet from HTML
An applet can receive parameters from the HTML web page with the tag that invokes <APPLET> using the method getParameter(“argument”). This method returns the value ‘argument’ as an object of class String. The parameters are transmitted inside the tag <APPLET> with <PARAM NAME = “argument” VALUE = “the value”>.
AWT, Applets, and Applications
The AWT provides basic components for a GUI (Graphical User Interface) and is used in applications and Java applets. One of the advantages of using AWT is that the interface is platform-independent. This ensures that what you see on one computer will look the same on another computer. A strategy to study AWT is to divide it into Components, Containers, Layouts (design managers), and Events.
Components
They are classes or interfaces that allow you to create graphical objects that comprise a GUI, such as buttons, dropdown lists, text boxes, checkboxes, radio buttons, text fields, labels, menus, etc. The following table introduces them briefly:
- Button: Used to receive the click of the mouse.
- Canvas: Canvas or drawing panel.
- Checkbox: Selects an item.
- CheckboxMenuItem: Verification within a menu.
- Choice: Static dropdown elements.
- Container: The parent of all containers.
- Dialog: A high-level dialog box window.
- Frame: Frame and class of all GUI windows.
- Label: Label or a chain of text.
- List: Dynamic component elements.
- Menu item: An item within a menu.
- Panel: Basic wrapper class to create designs.
- Scrollbar: To make a selection within a range of values.
- ScrollPane: Implements a horizontal and vertical slider for a single component.
- TextArea: Introduce a text or a rectangular block.
Layouts
They are classes or interfaces that allow you to create objects that manage the design, distribution, and placement of the component objects within container objects. For example, the FlowLayout distributes components from left to right and top to bottom, the BorderLayout distributes them in five geographic areas (north, south, east, west, and center), etc. The distribution of components in a container is usually controlled with a layout manager. Each container (like a Panel or Frame) has a default layout manager associated with it, which can be changed by invoking the method setLayout().
The following layout managers are included in the Java programming language:
- FlowLayout: This is the default layout manager on the Panel and the Applet. It places the components from left to right and from top to bottom.
- BorderLayout: This is the default layout manager of Window, Dialog, and Frame. It puts the components into 5 regions: North, South, East, West, and Center.
- GridLayout: A layout manager that provides flexibility to place components in cells like a spreadsheet.
- CardLayout: Is rarely used, and places components in layers where each layer is like a card, hence the name.
- GridBagLayout: Used to distribute the components as in a few cells but differing in size, it is similar to GridLayout.
Events
These are the classes or interfaces that allow you to create objects that capture and manage events. An event is an action on a component, for example, clicking a button, pressing the enter key on a button, moving an item using the navigation buttons, or special events scheduled by time, etc. Without events, GUIs would be lifeless graphical interfaces and therefore would not be very useful.
Event Sources
A source of an event is an event generator. For example, a mouse click on a button component generates an ActionEvent to the button as the origin or source of the event. The instance of ActionEvent is an object that contains information about the event that just occurred. This contains:
- getActionCommand(): Returns the command name associated with the action.
- getModifiers(): Returns any switch that has continued for the action.
The Event Delegation Model
The event delegation model appears with the JDK version 1.1. With this model, events are sent to the component where the event originated, but each component propagates the event to one or more classes called listeners. The listeners contain event handlers that receive and process the event. In this way, the event handler can be a separate object from the component. The listeners are classes that implement the EventListener interface.
Events are objects that report only to registered listeners. Each event has a corresponding listener interface that tells you what are the appropriate methods that should be defined within the class to receive these types of events. The class implementing the interface defines all these methods and can be registered as a listener.
Implementing Multiple Event Listeners Interfaces
AWT packages allow multiple listeners to be invoked by the same component. You can program code to handle multiple events in a single method. However, sometimes a design within an application requires many unrelated parties in the same program to react to the same event. This can happen if, for example, context-sensitive help is added to an existing program.
The listener mechanism lets you add a method called addXxxListener() as many times as needed, and you can specify many different listeners as your design requires. All these methods have their registered listener handlers that are invoked when an event happens.
Event Adapter
It is a lot of work to implement all methods in each of the listener interfaces, particularly in the case of the MouseListener and WindowListener interfaces. As a convenience, the Java programming language provides adapter classes that implement each interface containing more than one method. The methods in these adapter classes are empty. Thus, classes that you define as listeners can extend the adapter class and override only the methods you need.
Using AWT with Applets
As we can appreciate, the AWT provides the building blocks for a GUI and is used in applications and Java applets. Particularly in the case of applets, you can use almost all AWT classes except for Frame, Dialog, Window, Container, etc., except for the Menu class. In the next section, we present an example that makes extensive use of classes and interfaces in this package on a graphing of functions.