The java::bind Command


Usage:

java::bind javaObj
java::bind javaObj eventName
java::bind javaObj eventName script

The java::bind command is used to associate scripts with events fired by Java objects. The javaObj argument specifies a Java object. The eventName argument identifies an event fired by the object.

The events fired by Java objects are divided into event interfaces. Each event is represented by an event method in an event interface. For example, objects of the java.awt.Button class can fire events in the ActionListener, ComponentListener, FocusListener, KeyListener, MouseListener and MouseMotionListener interfaces (all these interfaces are defined in the JDK package java.awt.event.) The KeyListener interface in turn contains the event methods keyPressed, keyReleased and keyTyped. To find out what events are fired by different Java classes, please consult their API documentation.

The eventName argument for the java::bind command is the full or abbreviated name of the event. The full event name is the name of an event interface, followed by the character ".", followed by the name of the event method. For example, java.awt.event.KeyListener.keyTyped. The abbreviated event name is just the name of the event method. For example, keyTyped. Abbreviated event names may be used only if the event method name appears in exactly one of the event interfaces of the object.

If the script argument is given, it specifies a callback script to to be executed when the given event is fired in the javaObj. If a callback script already exists for the given event in javaObj, it will be replaced by script. If script is the empty string, any previously installed callback scripts for the given event in javaObj will be removed. See the Event Scripts section for more information.

If the script argument is not given, the java::bind command returns the current callback script, if any, for the given event in javaObj.

If the eventName and script arguments are both omitted, the java::bind command returns a Tcl list of the full names of all the events that are currently associated with callback scripts for the given javaObj.

Example:

This example creates a frame containing a single button widget. It then uses the java::bind command to associate a Tcl script that will be invoked when the ActionListener.actionPerformed interface method is invoked. When the user presses the button, the Tcl script will be evaluated inside the interpreter.

package require java
java::import -package java.awt Frame Button

set f [java::new Frame]
set b [java::new Button "Hi"]
$f setSize 70 70
$f add $b
$f show

java::bind $b actionPerformed {
    puts "Hi there"
}

Copyright © 1997-1998 Sun Microsystems, Inc.