home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Java AWT

Previous Appendix A
Using Properties and Resources

A.2 Server Properties

Java programs can read properties from any file to which they have access. Applications, of course, can open files on the platform where they execute; applets cannot. However, applets can read certain files from the server. Example A.1 is an applet that reads a properties file from its server and uses those properties to customize itself. This is a useful technique for developers working on commercial applets: you can deliver an applet to a customer and let the customer customize the applet by providing a property sheet. The alternative, having the applet read all of its customizations from HTML parameter tags, is a bit more clumsy. Server properties let you distinguish between global customizations like company name (which would be the same on all instances of the applet) and situation-specific customizations, like the name of the animation the user wants to display (the user may use the same applet for many animation sequences). The company name should be configured through a style sheet; the animation filename should be configured by using a <PARAM> tag.

Example A.1 uses a properties list to read a message and font information. Following the source is the actual property file. The property file must be in the same directory as the HTML file because we use getDocumentBase() to build the property file's URL. Once we have loaded the property list, we can use getProperty() to read individual properties. Unfortunately, in Java 1.0, we cannot use the Font class's methods to read the font information directly; getFont() can only read properties from the system property list. Therefore, we need to read the font size, name, and type as strings, and call the Font constructor using the pieces as arguments. Java 1.1 does a lot to fix this problem; we'll see how in the next section.

Example A.1: Getting Properties from a Server File

import java.util.Properties;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.MalformedURLException;
public class Prop extends java.applet.Applet {
    Properties p;
    String theMessage;
    public void init () {
        p = new Properties();
        try {
            URL propSource = new URL (getDocumentBase(), "prop.list");
            InputStream propIS = propSource.openStream();
        } catch (MalformedURLException e) {
            System.out.println ("Invalid URL");
        } catch (IOException e) {
            System.out.println ("Error loading properties");
    public void initFromProps (Properties p) {
        String fontsize = p.getProperty ("MyProg.font.size");
        String fontname = p.getProperty ("MyProg.font.name");
        String fonttype = p.getProperty ("MyProg.font.type");
        String message  = p.getProperty ("MyProg.message");
        int size;
        int type;
        if (fontsize == null) {
            size = 12;
        } else {
            size = Integer.parseInt (fontsize);
        if (fontname == null) {
            fontname = "TimesRoman";
        type = Font.PLAIN;
        if (fonttype != null) {
            boolean bold = (fonttype.indexOf ("bold") != -1);
            boolean italic = (fonttype.indexOf ("italic") != -1);
            if (bold) type |= Font.BOLD;
            if (italic) type |= Font.ITALIC;
        if (message == null) {
            theMessage = "Welcome to Java";
        } else {
            theMessage = message;
        setFont (new Font (fontname, type, size));
    public void paint (Graphics g) {
        g.drawString (theMessage, 50, 50);

The file prop.list :

MyProg.message=Hello World

Figure A.1 results from using this applet with this property file.

Previous Home Next
System Properties Book Index Resource Bundles

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java