AppletContext and showDocument( )
One application of Java is to use active images and animation to provide a graphical means
of navigating the Web that is more interesting than simple text-based links. To allow your
applet to transfer control to another URL, you must use the showDocument( ) method defined
by the AppletContext interface. AppletContext is an interface that lets you get information
from the applet's execution environment. The methods defined by AppletContext are
shown in Table 21-2. The context of the currently executing applet is obtained by a call
to the getAppletContext( ) method defined by Applet.
Within an applet, once you have obtained the applet's context, you can bring another
document into view by calling showDocument( ). This method has no return value and
throws no exception if it fails, so use it carefully. There are two showDocument( ) methods.
The method showDocument(URL) displays the document at the specified URL. The
method showDocument(URL, String) displays the specified document at the specified
location within the browser window. Valid arguments for where are "_self" (show in current
frame), "_parent" (show in parent frame), "_top" (show in topmost frame), and "_blank"
(show in new browser window). You can also specify a name, which causes the document
to be shown in a new browser window by that name.
The following applet demonstrates AppletContext and showDocument( ). Upon execution,
it obtains the current applet context and uses that context to transfer control to a file called
Test.html. This file must be in the same directory as the applet. Test.html can contain any
valid hypertext that you like.
/* Using an applet context, getCodeBase(),
and showDocument() to display an HTML file.
import java.awt.*;
import java.applet.*;
<applet code="ACDemo" width=300 height=50>
public class ACDemo extends Applet{
public void start() {
AppletContext ac = getAppletContext();
URL url = getCodeBase(); // get url of this applet
try {
ac.showDocument(new URL(url+"Test.html"));
} catch(MalformedURLException e) {
showStatus("URL not found");
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks