Java Event Scripts


A callback script is evaluated when a Java object fires an event. In most cases, the script does not need to return a value to the caller. There are some cases where a return value is needed. In such a case, the event method corresponding to the event has a non-void return type. The callback script should use the return command to return the desired value or Java object.

When a Java object fires an event, it may expect the callback script to throw an exception to indicate certain conditions. This can be done using the java::throw command. For example: the vetoableChange event requires the callback script to throw a java.beans.PropertyVetoException if the script deems the new value of a property unaccecptable, as in the following code fragment:

java::bind $b vetoableChange {
    if {[java::event propertyName] == "size"} {
        if {[java::event newValue] > 10} {
            java::throw [java::new java.beans.PropertyVetoException \
                "value too large" [java::event]]

The callback script can throw any unchecked exception object (e.g., instances of java.lang.Error or java.lang.RuntimeException.) The callback script can also throw any checked exception allowed by the signature of the event method.

If the callback script throws a checked exception which is not allowed for the event, a Tcl background error is generated. If the callback script is expected to return a value, an undefined value is returned to the Java object.

Copyright © 1997-1998 Sun Microsystems, Inc.