Home | | Object Oriented Programming | | The Complete Reference Java | | Internet Programming | | Web Programming | getDocumentBase( ) and getCodeBase( ) - The Applet Class

Chapter: Java The Complete Reference - The Java Library - The Applet Class

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

getDocumentBase( ) and getCodeBase( ) - The Applet Class

Often, you will create applets that will need to explicitly load media and text.

getDocumentBase( ) and getCodeBase( )

 

Often, you will create applets that will need to explicitly load media and text. Java will allow the applet to load data from the directory holding the HTML file that started the applet (the document base) and the directory from which the applet’s class file was loaded (the code base). These directories are returned as URL objects (described in Chapter 22) by getDocumentBase( ) and getCodeBase( ). They can be concatenated with a string that names the file you want to load. To actually load another file, you will use the showDocument( ) method defined by the AppletContext interface, discussed in

the next section.

 

The following applet illustrates these methods:

 

// Display code and document bases.

import java.awt.*;

 

import java.applet.*; import java.net.*;

/*

 

<applet code="Bases" width=300 height=50> </applet>

 

*/

 

public class Bases extends Applet {

// Display code and document bases.

public void paint(Graphics g) {

 

String msg;

 

URL url = getCodeBase(); // get code base

msg = "Code base: " + url.toString(); g.drawString(msg, 10, 20);

 

url = getDocumentBase(); // get document base

msg = "Document base: " + url.toString(); g.drawString(msg, 10, 40);

 

}

 

}

Sample output from this program is shown here:



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.