[ Back | Previous | Next ]

How to use java.lang.reflect.Field for Applet params?

Package:
java.lang.reflect.*
Product:
JDK
Release:
1.1
Related Links:
General
Field
Method
Comment:
	ParamResource pr = new ParamResource();
	String str = null;
	Class t = this.getClass();
	/*
	Field[] f = t.getDeclaredFields();
	for (int j = 0; j < f.length; j++) {
	dbg("fields: " + f[j]);
	}
	*/
	for (Enumeration e = pr.getKeys(); e.hasMoreElements();) {
		str = (String) e.nextElement();
		dbg("" + str);
		try {
			Field field = t.getDeclaredField(str);
			dbg("FOUND FIELD: " + str + "==" + field.getType());
			if (field.getType() == java.awt.Color.class) {
				dbg("COLOR" + field.get(this));
				field.set(this, new Color(Integer.parseInt(getParameter(str, (String) pr.handleGetObject(str)), 16)));
				//field.set( this, new java.awt.Color(45,45,245) );

			} else
				if (field.getType().getName().equals("int")) {
					dbg("INTEGER");
					field.setInt(this, Integer.valueOf(getParameter(str, (String) pr.handleGetObject(str))).intValue());
				} else
				if (field.getType().getName().equals("boolean")) {
					dbg("BOOLEAN");
					field.setBoolean(this, Boolean.valueOf(getParameter(str, (String) pr.handleGetObject(str))).booleanValue());
				} else
					if (field.getType() == java.lang.String.class) {
						dbg("String");
						field.set(this, getParameter(str, (String) pr.handleGetObject(str)));
					}
			dbg("" + str + ": " + field.get(this));
		} catch (Exception ee) {
			ee.printStackTrace();
		}
	}

ParamResource

Param resource would contain all the params for the applet and their default value.
import java.awt.*;
/**
 * This type was created in VisualAge.
 */
public class ParamResource extends java.util.ListResourceBundle {
	static final Object[][] contents = {
	// LOCALIZE THIS
	{"fontcolor", "ffffff"}, 
	{"bgcolor", "333333" }, 
	{"speed",  "50"},
	{"font", "Verdana" },
	{"target", "_self" },
	{"newslink", "http://bananaman"},
	{"fontstyle", "1"}, 
	{"maxlines", "10"}, 
	{"vertical", "true"}, 
	{"alignment", ""+java.awt.FlowLayout.CENTER}, 
	{"fontsize", "12"} // real object, not just string
	// END OF MATERIAL TO LOCALIZE
	};
/**
 * ParamResource constructor comment.
 */
public ParamResource() {
	super();
}
	protected Object[][] getContents() {
		return contents;
	}
}
1