Previous | Next | Trail Map | Writing Applets | Creating an Applet User Interface


Creating a GUI

Almost all applets have a graphical user interface (GUI). The Creating a User Interface(in the Creating a User Interface trail)trail gives many examples of applet GUIs. This page discusses the few issues that are particular to applet GUIs.
An Applet is a Panel.
Because Applet is a subclass of the AWT Panel class, applets can contain other Components, just as any Panel can. As Panels, Applets also participate in the AWT drawing and event hierarchy.

Applets appear in pre-existing browser windows.
This has two implications. First, unlike GUI-based applications, applets don't have to create a window to display themselves in. (They can, if they have a good reason, but they usually just display themselves within the browser window.) Second, depending on the browser implementation, your applet's components might not be shown unless your applet calls validate() after adding components to itself. Fortunately, calling validate() can't hurt.

Each applet has a user-specified, pre-determined size.
Because the <APPLET> tag requires that the applet's width and height be specified, and because browsers don't necessarily allow applets to resize themselves, applets must make do with a fixed amount of space that might not be ideal. Even if the amount of space is ideal for one platform, the platform-specific parts of the applet (such as buttons) might require a different amount of space on another platform. You can compensate by recommending that pages that include your applet specify a little more space than might be necessary, and by using flexible layouts, such as GridBagLayout and BorderLayout, that adapt well to extra space.

Applets load images using the Applet getImage() methods.
The Applet class provides a convenient form of getImage() that lets you specify a base URL as one argument, followed by a second argument that specifies the image file location, relative to the base URL. The Applet getCodeBase() and getDocumentBase() methods provide the base URLs that most applets use. Images that an applet always needs, or needs to rely on as a backup, are usually specified relative to where the applet's code was loaded from (the code base). Images that are specified by the applet user (often with parameters in the HTML file) are usually relative to the page that includes the applet (the document base).

Applet classes (and often the data files they use) are loaded over the network, which can be slow.
Applets can do several things to decrease the perceived startup time. Their Applet subclass can be a small one that immediately displays a status message. And, if some of the applet's classes or data aren't used right away, the applet can preload the classes or data by referencing them before they're needed. For example, the AppletButton class, at the beginning of its main thread, gets the class object for the window the button will bring up. Its main purpose is to make sure the class is valid, but an added benefit is that getting the class object forces the class file to be loaded before it's needed, which makes instantiating the class much quicker than if the class still had to be loaded.


Previous | Next | Trail Map | Writing Applets | Creating an Applet User Interface