Download the JDK
Search the Tutorials
Hide the TOC
Trail: Deployment
Lesson: Applets
Section: Taking Advantage of the Applet API
Applets
Getting Started with Applets
Importing Classes and Packages for Applets
Defining an Applet Subclass
The Life Cycle of an Applet
Methods for Milestones
Methods for Drawing and Event Handling
Methods for Adding UI Components
What Applets Can and Can't Do
Taking Advantage of the Applet API
Finding and Loading Data Files
Displaying Short Status Strings
Displaying Documents in the Browser
Sending Messages to Other Applets
Playing Sounds
Defining and Using Applet Parameters
Deciding Which Parameters to Support
Writing the Code to Support Parameters
Giving Information about Parameters
Using the APPLET Tag
Practical Considerations When Writing Applets
Security Restrictions
Creating a User Interface
Creating a GUI
Displaying Diagnostics to the Standard Output and Error Streams
Getting System Properties
Threads in Applets
Threads in Applets: Examples
Working with a Server-Side Application
A Simple Network Client Applet
Using a Server to Work Around Security Restrictions
Finishing an Applet
Before You Ship that Applet
The Perfectly Finished Applet
Deploying Applets
General Deployment Considerations
Using the applet Tag
Using the object Tag
Using the embed Tag
Deploying Applets in a Mixed-Browser Environment
Solving Common Applet Problems
Questions and Exercises
Home Page > Deployment > Applets
Displaying Documents in the Browser

> Have you ever wanted an applet to display formatted HTML text? There's an easy way to do it: Ask the browser to display the text for you.

> With the `AppletContext` `showDocument` methods, an applet can tell the browser which URL to show and in which browser window.

> Here are the two forms of `showDocument`:

> >     public void showDocument(java.net.URL url)
> >     public void showDocument(java.net.URL url, String targetWindow)

> The one-argument form of `showDocument` simply tells the browser to display the document at the specified URL, without specifying the window to display the document in.

> > Terminology Note: In this discussion, frame refers not to a Swing `JFrame`, but to an HTML frame within a browser window.

> The two-argument form of `showDocument` lets you specify which window or HTML frame to display the document in. The second argument can have the values listed below.

> `"_blank"`
>   Display the document in a new, nameless window.
> `"windowName"`
>   Display the document in a window named windowName. This window is created if necessary.
> `"_self"`
>   Display the document in the window and frame that contain the applet.
> `"_parent"`
>   Display the document in parent frame of the applet's frame. If the applet frame has no parent frame, this acts the same as `"_self"`.
> `"_top"`
>   Display the document in the top-level frame. If the applet's frame is the top-level frame, this acts the same as `"_self"`.

>   The following applet lets you try every option of both forms of `showDocument`. The applet brings up a window that lets you type in a URL and choose any of the `showDocument` options. When you press Return or click the Show document button, the applet calls `showDocument`.

>   > Note: If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the Java(TM) SE JRE or JDK. This applet requires JDK 1.4 or later. You can find more information on the Java Plug-in home page.

>   Following is the applet code that calls `showDocument`. (Here's the `whole program`.)

>   >             ...//In an Applet subclass:
>   >             urlWindow = new URLWindow(getAppletContext());
>   >             . . .

>   >     class URLWindow extends Frame {
>   >         . . .
>   >         public URLWindow(AppletContext appletContext) {
>   >             . . .
>   >             this.appletContext = appletContext;
>   >             . . .
>   >         }
>   >         . . .
>   >         public boolean action(Event event, Object o) {
>   >             . . .
>   >                 String urlString = /* user-entered string */;
>   >                 URL url = null;
>   >                 try {
>   >                     url = new URL(urlString);
>   >                 } catch (MalformedURLException e) {
>   >                     ...//Inform the user and return...
>   >                 }

>   >                 if (url != null) {
>   >                     if (/* user doesn't want to specify the window */) {
>   >                         appletContext.showDocument(url);
>   >                     } else {
>   >                         appletContext.showDocument(url,
>   >                                                    /* user-specified window */);
>   >                     }
>   >                 }
>   >             . . .

Previous page: Displaying Short Status Strings
Next page: Sending Messages to Other Applets