Java Applets: AppletContext Interface

The Applet API provides an interface, java.applet.AppletContext, which can be used to interact with the environment where the applet runs. For example, we can manipulate the content of a web page, load a URL, interact with the JavaScript code embedded in the web page, communicate with other applets running in the same web page, and so on.

An AppletContext object is obtained using getAppletContext() on the Applet object. The prototype declaration of this method is as follows:

public java.applet.AppletContext getAppletContext();

The following code shows how to obtain an AppletContext object.

AppletContext ac = getAppletContext();

The AppletContext interface provides many useful methods, some of which are mentioned as follows:

AudioClip getAudioC.5lip(URL url);

Returns an AudioClip object from the specified URL

Image getImage(URL url);

Returns an Image object, which can be painted on the screen, from the specified URL

Applet getApplet(String appletName)

Finds and returns an Applet object that exist in the same context, with the specified name

Enumeration getApplets();

Finds and returns all applets that exist within the same context

void showDocument(URL url)

Loads the specified URL in the current window

void showDocument(URL url, String targetWindow)

Loads the specified URL in the specified window

void showStatus(String message)

Displays a short message on the status bar

void setStream(String key, InputStream stream)

Binds the specified stream with the specified key in this applet context

InputStream getStream(String key);

Returns the stream associated with the specified key within this applet context

Iterator getStreamKeys();

Returns a list of keys of the streams in this applet context

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *