Application Techniques

Obtaining the Control with Focus

How to obtain the name of the form control that currently has focus.

About this technique



Java Client Techniques> General techniques for controls


You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

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.

Basic steps for adding a focus listener   Top of page

  1. In the Programming Editor, add the Java Interface java.awt.event.AWTEventListener.

  2. In the formActivate() method, call the addAWTEventListener() using the FOCUS_EVENT_MASK.

  3. In the eventDispatched event, check for an instance of a focus event, and handle the component as required. This example just displays the name of the component.

  4. In the windowClosing event, remove the listener.

Adding an interface   Top of page

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.

Adding the listener   Top of page

Toolkit is the abstract superclass of AWT containers. The AWTEventListener is a special-purpose listener for low-level events.

     (AWTEventListener) this, AWTEvent.FOCUS_EVENT_MASK); 

Notes on the code

Processing the focus event and getting the control name   Top of page

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(); 

Notes on the code

Removing the listener   Top of page

This code removes the listener before the window closes.

     (AWTEventListener) this); 

Notes on the code

Copyright © 2000, SilverStream Software, Inc. All rights reserved.