Java Applets: Loading Web Pages

You can load a web page in the browser’s window using the showDocument() method on the AppletContext object. It takes the following forms:

void showDocument(URL url)

void showDocument(URL url, String targetWindow)

The first form takes a URL argument and loads it in the current window. The second form allows us to load the URL in the specified window. The second argument can have the following values:

  • _blank — Loads the URL in a new unnamed window
  • targetWindow — Loads the URL in the window having the name targetWindow
  • _self — Loads the URL in the window containing this applet
  • _parent — Loads the URL of the parent window containing this applet
  • top — Loads the URL in the top-level window.

The following applet loads the URL http://www.yahoo.com in the applet’s window.

/LoadDemo.java import java.awt.*;

import java.applet.*;

import java.net.*;

public class LoadDemo extends Applet {

AppletContext ac; URL url; public void start() {

ac = getAppletContext();

url = getCodeBase();

}

public void paint(Graphics g) {

try {

ac.showDocument(new URL(“http://www.yahoo.com”));

}catch(Exception e) {showStatus(“URL not found”);}

}

}

The following applet declaration results in the output as shown in Figure 16.19:

<applet code=”LoadDemo” width=”200″ height=”80″>

</applet>

Figure 16.19: Loading a URL using applet

Reproduced with permission of Yahoo. © 2014 Yahoo. YAHOO! and the YAHOO! logo are registered trademarks of Yahoo.

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 *