Table of Contents
Previous Section
If you write an applet, or aquire the source code for an applet, you will probably want to follow this procedure to give the applet the associative behavior it needs to be a client-side component.
public class MyApplet extends Applet implements SimpleAssociationDestination { // .... }
public Vector keys() { Vector keys = new Vector(1); keys.addElement("title"); return keys; }
synchronized public Object valueForKey(String key) { if (key.equals("title")) { return this.getLabel(); } } synchronized public void takeValueForKey(Object value, String key) { if (key.equals("title")) { if ((value != null) && !(value instanceof String)) { System.out.println("Object value of wrong type set for key 'title'. Value must be a String."); } else { self.setLabel(((value == null) ? "" : (String)value)); } }
You should be able to access the keys directly or, ideally, through accessor methods ("getLabel()" and "setLabel()" in the above example). It is a good idea to use the synchronize modifier with takeValueForKey(Object, String) and valueForKey(String) because these methods can be invoked from other threads to read or set data.
The remaining steps apply only if the applet has an action.
protected Association _assoc; // ... synchronized public void setAssociation(Association assoc) { _assoc = assoc; }
The Association object must be stored so it can be used later as the receiver of the invokeAction() message. The Association forwards the action to the AppletGroupController, which handles the invocation of the server-side action method.
synchronized public boolean action(Event evt, Object what) { if (_assoc != null) { _assoc.invokeAction("action"); } return true; }