How to obtain the name of the form control that currently has focus.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on form basics in the Programmer's Guide For more about listeners, see Adding a Listener to a Form Control |
The example implements an AWT listener for a focus event. By adding the listener to the form, it responds to a focus change for any component on the form. When the user tabs or clicks among the form controls, the name of the control with focus is displayed in the text field at the bottom of the form.
java.awt.event.AWTEventListener
.
formActivate()
method, call the addAWTEventListener()
using the FOCUS_EVENT_MASK.
In the Programming Editor, you specify that the form implements an interface by selecting File>Java Interfaces. You must specify the fully qualified package name for the interface. For AWT events, it is java.awt.event.AWTEventListener
.
Be sure to check Create stubs for interface methods so that the methods you must implement for the interface are added to the form's code.
Toolkit is the abstract superclass of AWT containers. The AWTEventListener is a special-purpose listener for low-level events.
Toolkit.getDefaultToolkit().addAWTEventListener( (AWTEventListener) this, AWTEvent.FOCUS_EVENT_MASK);
This code from the eventDispatched event checks the type of the event object and looks for the event ID FOCUS_GAINED. Then it displays the name of the component in the fldWithFocus control.
if (aWTEvent1 instanceof FocusEvent && aWTEvent1.getID() == FocusEvent.FOCUS_GAINED) { FocusEvent focusEvent = (FocusEvent) aWTEvent1; Component componentFocus = (Component) aWTEvent1.getSource(); fldWithFocus.setText(componentFocus.getName()); }
getID()
and getSource()
methods are methods of the AWTEvent object passed to the event. Individual event IDs for specific events are defined in subclasses of AWTEvent. The event IDs we are interested here are constants defined in FocusEvent.
getName()
method for a component object reports the name of the component's instance variable.
This code removes the listener before the window closes.
Toolkit.getDefaultToolkit().removeAWTEventListener( (AWTEventListener) this);