A script-enabled browser is required for this page to function properly.

Focus and Key Events for Bean Area Components

For bean area components in a Forms applet, the developer must add functionality so that the bean area components can enjoy focus and navigation consistent with other Forms components. The bean area (bean wrapper class) must implement the FocusListener and KeyListener interfaces and all of the components in a bean area must also register the bean area as a FocusListener and KeyListener. Finally, the developer must implement methods that pass registered properties (from the VBean class) and the event object to create focus and navigation consistent with other Forms components.

Focus Events

The server does not automatically know about the input focus changes within a bean area. For the server to know a component in a bean area has the focus, the developer must use the registered property FOCUS_EVENT, defined in VBean. Using FOCUS_EVENT, bean area components can achieve mouse focus consistent with the rest of the components in a Forms applet. This allows the user to call help that is context-sensitive to the bean area.

How to Use the FOCUS_EVENT Registered Property

The following are the initial requirements for using FocusListener and the registered property FOCUS_EVENT:

When a component in a bean area gets the focus, invoke the mHandler.setProperty() method with the registered property FOCUS_EVENT and the JDK’s FocusEvent as its arguments. FocusEvent is a registered property defined in VBean. The mHandler.setProperty() method tells the server that the bean area has the focus. The server does not track the focus to the individual component level of bean areas.

Focus Events Code Example

This section describes the use of the FocusListener interface and FOCUS_EVENT registered property using code snippets. The complete sample class code is included at the end of the this section.

To use the FocusListener interface:

Implement FocusListener. Note that the class must also extend VBean. This example class is called BeanWrapper.

 
 public class BeanWrapper
        extends VBean
        implements FocusListener, KeyListener
 {
     // the Handler object passed to the view by Forms
     private IHandler mHandler; 
	 

Create the components. This example bean area includes four push buttons for four user choices.

 
     PushButton beanButton1 = new PushButton("BeanButton1");
     PushButton beanButton2 = new PushButton("BeanButton2");
     PushButton beanButton3 = new PushButton("BeanButton3");
     PushButton beanButton4 = new PushButton("BeanButton4");
 

Initialize the class, registering the FocusListener for each component. Note that addFocusListener is set for the bean area and each of the push buttons.

 
     // initialise the class
     public void init(IHandler handler)
     {
         super.init(handler);
         mHandler = handler;
         add(beanButton1,BorderLayout.NORTH);
         add(beanButton2,BorderLayout.EAST);
         add(beanButton3,BorderLayout.WEST);
         add(beanButton3,BorderLayout.SOUTH);
 
         addFocusListener(this);
 
         beanButton1.addFocusListener(this);
         beanButton2.addFocusListener(this);
         beanButton3.addFocusListener(this);
         beanButton4.addFocusListener(this);
     }

The focusGained() method gets called when the bean area or any of its children (components) gets the focus. FocusEvent, the standard JDK event object, is passed into the focusGained() method. The following is a typical example of the focusGained() method.

The beanButton1.requestFocus() method assigns the focus to the first component in the bean area when the bean area (wrapper) gets the focus. Invoke the mHandler.setProperty() method using the registered property FOCUS_EVENT and the event object (e) to tell the server that the bean area has the focus.

 
     public void focusGained(FocusEvent e)
     {
         if (e.getComponent() == this)
         {
             beanButton1.requestFocus();
         }
 
         try
         {
             mHandler.setProperty(FOCUS_EVENT, e);
         }
         catch ( Exception ex )
         {
         }
     }
 
 
     public void focusLost(FocusEvent e)
     {
     } 

Key Events

Keystrokes from form items and bean area items in a Forms applet should produce consistent results. By default, Ctrl+K invokes the runtime Keys help screen for the form item that has focus. Tab moves the input focus from the current to the next form item. Shift+PageDown moves the input focus to the first form item in the next block. By default, however, keystrokes on bean area items do not produce these results.

To maintain consistent input focus and keystroke navigation in a Forms applet that contains a bean area, send most keystrokes from a bean area to the server using the KeyListener, KEY_EVENT registered property, and the mHandler.setProperty(KEY_EVENT, e) method. The server then performs the appropriate action. For example, pressing Ctrl+K causes the server to display the runtime Keys help screen for the bean area that has focus and pressing Shift+PageDown causes the server to move the focus to the first form item in the next block.

In some bean area keyboard navigation, however, it is important to not send certain keystrokes—some instances of Tab and Shift+Tab—to the server, depending on the position of the component in the bean area. The general rule is to send all bean area keystrokes to the server that should move the focus from the bean area. For keystrokes that move the focus between components in the same bean area, for example, Tab and Shift+Tab, do not send the keystrokes to the server.

Sending Tab and Shift+Tab keystrokes from a bean area component to the server causes the server to move the focus from the bean area to the next or previous form item respectively. With this in mind, there are situations where Tab and Shift+Tab keystrokes should be sent to the server: for the first component in a bean area, send Shift+Tab to the server, and for the last component in a bean area, send Tab to the server.

It is the responsibility of the developer to program what keystrokes are sent and what keystrokes are not sent to the server for components in a bean area.

Example

Background:

A form contains several items. A bean area is one of these items and it is located between other items. The bean area contains 4 push buttons.

Objective:

The user should be able to invoke standard form actions such as COMMIT and NEXT BLOCK using keystrokes from the bean area. The user should be able to press Tab to move the focus to the next push button in the bean area and press Shift+Tab to move the focus to the previous item in the bean area. When the user presses Tab from the last push button in the bean area, the focus should move to the next form item. When the user presses Shift+Tab from the first push button in the bean area, the focus should move to the previous form item.

Default Behavior:

Without implementing KeyListener, the KEY_EVENT registered property, and invoking the mHandler.setProperty(KEY_EVENT, e) method for the bean area components, no keystrokes are sent to the server. Tab and Shift+Tab move the input focus between bean area components. The server does not receive any keystrokes when keys are pressed to move focus from the bean area (for example, Shift+PageDown), so the form item with the apparent new focus may not really have the focus at the server. Pressing Ctrl+K for runtime Keys help displays runtime Keys help for the item the server identifies as having focus, which might be a different form item than the one that appears to have the focus. The next navigation keystroke, for example Tab, might produce a different result than expected because the navigation starts from the form item that the server identifies as having the focus. When keystrokes are not sent to the server, the server and client get out of sync on what is the current item.

Solution:

Implement KeyListener, use the KEY_EVENT registered property, and invoke the mHandler.setProperty(KEY_EVENT, e) method to send most keystrokes to the server. Choose to send or not to send Tab and Shift+Tab keystrokes for each of the push buttons to the server to move the focus as indicated in the objective.

Push Button 1:

When the user presses Tab from the last form item before the bean area, push button 1 in the bean area gets the focus. When the user presses Tab from push button 1, push button 2 gets the focus. The Tab keystroke should not be sent to the server, because sending the Tab keystroke causes the server to move the focus from the bean area to the next form item after the bean area.

When the user presses Shift+Tab from push button 1, the form item before the bean area must get the focus. The Shift+Tab keystrokes must be sent to the server, because sending the Shift+Tab keystrokes causes the server to move the focus to the previous form item. If the Shift+Tab keystrokes are not sent to the server, the server and client get out of sync on what is the current item.

Push Buttons 2 and 3 :

When the user presses Tab from push button 2, push button 3 gets the focus and again, the keystroke must not be sent to the server. (The same condition applies for pressing Tab from push button 3.) If the user presses Shift+Tab from push button 2, 3, or 4, the previous push button in the bean area gets the focus. These keystrokes should not be sent to the server. Whenever the focus is lost by one bean area component and gained by another, Tab and Shift+Tab keystrokes should not be sent to the server.

Push Button 4:

When the user presses Tab from push button 4, the last push button in the bean area, the keystroke must be sent to the server, because sending the Tab keystroke causes the server to move the focus from the bean area to the next form item. When the user presses Shift+Tab from push button 4 in the bean area, push button 3 gets the focus. Because the focus does not leave the bean area, the Shift+Tab keystrokes must not be sent to the server.

Summary

If the focus is on an element in the bean area and the user presses keystrokes that should move the focus out the bean area, send those keystrokes to the server. If the focus is on an element in the bean area and the user presses keystrokes that should move the focus to another element within that bean area, do not send those keystrokes to the server.

What keystrokes are not sent to the server depend on the element’s position in the bean area.

How to Use the KEY_EVENT Registered Property

To send the necessary keystrokes to the server for bean area components in a Forms applet, the developer must first implement the KeyListener interface:

The following are the initial requirements for using KeyListener:

With all components having registered KeyListener to the bean area, send all keystrokes except certain instances of Tab and/or Shift+Tab to the server depending on whether the keystrokes move the focus out of the bean area. Use the keyPressed() method of the KeyListener interface to filter keystrokes. For keystrokes that need to be sent to the server, the developer must invoke the mHandler.setProperty() method with the registered property KEY_EVENT (defined in VBean) and the JDK’s KeyEvent as its arguments.

Key Events Code Example

This section explains the use of the KeyListener interface using code snippets. This example also includes FocusListener information. The complete sample class code is included at the end of the this section.

To use the KeyListener interface:

Implement KeyListener. Note that the class must also extend VBean. This example class is called BeanWrapper.

 
 public class BeanWrapper
        extends VBean
        implements FocusListener, KeyListener
 {
     // the Handler object passed to our view by Forms
     private IHandler mHandler;
 

Create the components. This example bean area includes four push buttons.

 
     PushButton beanButton1 = new PushButton("BeanButton1");
     PushButton beanButton2 = new PushButton("BeanButton2");
     PushButton beanButton3 = new PushButton("BeanButton3");
     PushButton beanButton4 = new PushButton("BeanButton4");
 

Initialize the class, implementing the KeyListener for each component. Note that addKeyListener is invoked on each of the push buttons.

 
     // initialise the class
     public void init(IHandler handler)
     {
         super.init(handler);
         mHandler = handler;
         add(beanButton1,BorderLayout.NORTH);
         add(beanButton2,BorderLayout.EAST);
         add(beanButton3,BorderLayout.WEST);
         add(beanButton3,BorderLayout.SOUTH);
 
         addFocusListener(this);
 
         beanButton1.addFocusListener(this);
         beanButton2.addFocusListener(this);
         beanButton3.addFocusListener(this);
         beanButton4.addFocusListener(this);
 
         beanButton1.addKeyListener(this);
         beanButton2.addKeyListener(this);
         beanButton3.addKeyListener(this);
         beanButton4.addKeyListener(this);
     }
 

The keyPressed() method gets called when any component in the bean area has the focus and the user presses any key. KeyEvent, the standard JDK key event object, is passed into the keyPressed() method.

The following is a typical example of the keyPressed() method used with several components in a bean area.

The conditions that follow include all of the push buttons in the bean area. For the first component of the bean area, send all keystrokes except Tab to the server. But do send the Shift+Tab keystrokes to the server to move the focus back out of the bean area and send all other keystrokes that invoke standard form actions such as displaying the runtime Keys help screen (Ctrl+K) and NEXT BLOCK (Shift+PageDown).

The first condition describes the event for the first component in the bean area, beanButton1. The e.getSource() method tests whether beanButton1 is pressed. The e.getKeyCode() method tests whether a keystroke is Tab (KeyEvent.VK_TAB). If the key sequence pressed does not include Tab, in the else statement, invoke the mHandler.setProperty() method using the registered property KEY_EVENT and the event object (e) to send the keystrokes to the server.

If Shift+Tab is pressed, invoke the mHandler.setProperty() method using the registered property KEY_EVENT and the event object (e) to send the keystrokes to the server.

 
     public void keyPressed(KeyEvent e)
     {
         if ( e.getSource() == beanButton1 )
         {
             /*
             ** For the first component in the bean area, send all
             ** keystrokes except Tab to the server. Also, send 
             ** Shift+Tab to the server.
             */
             if ( (e.getKeyCode() == KeyEvent.VK_TAB) )
             {
                 if (e.isShiftDown() )
                 {
                     try
                     {
                         mHandler.setProperty(KEY_EVENT, e);
                     }
                     catch ( Exception ex )
                     {
                     }
                 }
             }
             else
             {
                 try
                 {
                     mHandler.setProperty(KEY_EVENT, e);
                 }
                 catch ( Exception ex )
                 {
                 }
             }
         }
 

This condition describes the event for the last component (beanButton4) in the bean area. The e.getSource() method tests whether beanButton4 is pressed. The e.getKeyCode() method tests whether the key sequence is not Shift+Tab. If the key sequence is not Shift+Tab, invoke the mHandler.setProperty() method using the registered property KEY_EVENT and the event object (e) to send the key sequence to the server.

 
         else if ( e.getSource() == beanButton4 )
         {
             /*
             ** For the last component in the bean area, send all
             ** keystrokes except Shift+Tab to the server.
             */
             if ( !((e.getKeyCode() == KeyEvent.VK_TAB) && (e.isShiftDown())) )
             {
                 try
                 {
                     mHandler.setProperty(KEY_EVENT, e);
                 }
                 catch ( Exception ex )
                 {
                 }
             }
         }
 

For the remaining components of the bean area that are neither the first nor last components, send all keystrokes except Tab and Shift+Tab to the server. This example code is for the next components in the bean area, beanButton2 and beanButton3.

The e.getSource() method tests whether beanButton2 or beanButton3 is pressed. The e.getKeyCode() method tests whether the keystroke is not a Tab (KeyEvent.VK_TAB). If the keystroke is not Tab, invoke the mHandler.setProperty() method using the registered property KEY_EVENT and the event object (e) to send the keystrokes to the server.

 
         else if ( (e.getSource() == beanButton2) ||
                   (e.getSource() == beanButton3) )
         {
             /*
             ** For all the remaining components in the bean area,
             ** send all keystrokes except Tab and Shift+Tab
             ** to the server.
             */
             if ( !(e.getKeyCode() == KeyEvent.VK_TAB) )
             {
                 try
                 {
                     mHandler.setProperty(KEY_EVENT, e);
                 }
                 catch ( Exception ex )
                 {
                 }
             }
         }
 }

Key Events for One Component in a Bean Area

If your bean area contains only one component (for example, beanButton1), send all key strokes on the component to the server. You must implement the KeyListener and use the keyPressed() method, but you can execute the mHandler.setProperty() method immediately.

The following example shows the code for the keyPressed() method for a single push button, beanButton1, in a bean area.

 
 public void keyPressed(KeyEvent e)
 {
     try
     {
         mHandler.setProperty(KEY_EVENT, e);
     }
     catch ( Exception ex )
     {
     }
 }

Complete Code Example for Focus and Key Events

 
 package oracle.forms.demos;
 
 import oracle.forms.properties.ID;
 import oracle.forms.handler.IHandler;
 import oracle.forms.ui.CustomEvent;
 import oracle.forms.properties.ID;
 import oracle.forms.handler.IHandler;
 import oracle.forms.ui.VBean;
 import oracle.ewt.button.PushButton;
 import java.awt.Component;
 import java.awt.event.FocusEvent;
 import java.awt.event.FocusListener;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
 import java.awt.BorderLayout;
 
 public class BeanWrapper
        extends VBean
        implements FocusListener, KeyListener
 {
     // the Handler object passed to the view by Forms
     private IHandler mHandler;
 
 
     PushButton beanButton1 = new PushButton("BeanButton1");
     PushButton beanButton2 = new PushButton("BeanButton2");
     PushButton beanButton3 = new PushButton("BeanButton3");
     PushButton beanButton4 = new PushButton("BeanButton4");
 
     public BeanWrapper()
     {
         super();
     }
 
     // initialise the class
     public void init(IHandler handler)
     {
         super.init(handler);
         mHandler = handler;
         add(beanButton1,BorderLayout.NORTH);
         add(beanButton2,BorderLayout.EAST);
         add(beanButton3,BorderLayout.WEST);
         add(beanButton3,BorderLayout.SOUTH);
 
         addFocusListener(this);
 
         beanButton1.addFocusListener(this);
         beanButton2.addFocusListener(this);
         beanButton3.addFocusListener(this);
         beanButton4.addFocusListener(this);
 
         beanButton1.addKeyListener(this);
         beanButton2.addKeyListener(this);
         beanButton3.addKeyListener(this);
         beanButton4.addKeyListener(this);
     }
 
     public void focusGained(FocusEvent e)
     {
         if (e.getComponent() == this)
         {
             beanButton1.requestFocus();
         }
 
         try
         {
             mHandler.setProperty(FOCUS_EVENT, e);
         }
         catch ( Exception ex )
         {
         }
     }
 
 
     public void focusLost(FocusEvent e)
     {
     }
 
 
     /*
     ** If a particular key sequence causes the navigation to stay inside
     ** the bean area i.e. one the components of the bean area loses
     ** focus and the other component of bean area gains focus, then
     ** we do not want to send the keystrokes to the server. Otherwise,
     ** we do want to send the keystrokes to the server.
     **
     ** A key sequence is sent to the server by invoking
     ** mHandler.setProperty(KEY_EVENT, e).
     **
     ** For example, if the focus is on the first component(beanButton1) of
     ** the bean area, and if the user presses Tab, then the focus will be
     ** transferred to the second component(beanButton2) of the bean area. 
     ** Since beanButton2 is inside the bean area, we do not want to send the 
     ** keystrokes to the server. If instead of Tab the user presses 
     ** Shift+Tab, then the focus needs to transferred to the previous form 
     ** item, which is outside the bean area. In this case, we need to send 
     ** the keystrokes to the server so that the server can determine the 
     ** previous form item to which the focus should be transferred.
     **
     ** Similarly, if the focus is on  the last component(beanButton4) of the
     ** bean area, and if the user presses Tab, then the focus needs to be
     ** transferred to the next form item, which is outside the bean area.
     ** In this case, we do want to send the keystrokes to the server so
     ** that it can determine the next form item to which the focus should be
     ** transferred. If instead of Tab the user presses Shift+Tab, then
     ** the focus needs to be transferred to the previous component
     ** (beanButton3) in the bean area. Since beanButton3 is inside the bean 
     ** area, we do not need to send the keystrokes to the server.
     **
     ** For other components in the bean area, such as beanButton2 and 
     ** beanButton3, we do not want to send Tab and Shift+Tab to the form
     ** server. This is because both of them cause focus to be transferred
     ** to a bean area component. Anything else should be sent to the
     ** server.
     */
     public void keyPressed(KeyEvent e)
     {
         if ( e.getSource() == beanButton1 )
         {
             /*
             ** For the first component in the bean area, send all
             ** keystrokes except Tab to the server. Also, send 
             ** Shift+Tab to the server.
             */
             if ( (e.getKeyCode() == KeyEvent.VK_TAB) )
             {
                 if (e.isShiftDown() )
                 {
                     try
                     {
                         mHandler.setProperty(KEY_EVENT, e);
                     }
                     catch ( Exception ex )
                     {
                     }
                 }
             }
             else
             {
                 try
                 {
                     mHandler.setProperty(KEY_EVENT, e);
                 }
                 catch ( Exception ex )
                 {
                 }
             }
         }
         else if ( e.getSource() == beanButton4 )
         {
             /*
             ** For the last component in the bean area, send all
             ** keystrokes except Shift+Tab to the server.
             */
             if ( !((e.getKeyCode() == KeyEvent.VK_TAB) && (e.isShiftDown())) )
             {
                 try
                 {
                     mHandler.setProperty(KEY_EVENT, e);
                 }
                 catch ( Exception ex )
                 {
                 }
             }
         }
         else if ( (e.getSource() == beanButton2) ||
                   (e.getSource() == beanButton3) )
         {
             /*
             ** For all the remaining components in the bean area,
             ** send all keystrokes except Tab and Shift+Tab
             ** to the server.
             */
             if ( !(e.getKeyCode() == KeyEvent.VK_TAB) )
             {
                 try
                 {
                     mHandler.setProperty(KEY_EVENT, e);
                 }
                 catch ( Exception ex )
                 {
                 }
             }
         }
     }
 
 
     public void keyTyped(KeyEvent e)
     {
     }
 
     public void keyReleased(KeyEvent e)
     {
     }
 
 
     public boolean setProperty(ID pid, Object value)
     {
         return super.setProperty(pid,value);
     }
 
     public String toString()
     {
         return ("BeanArea Wrapper");
     }
 }

How to Add JavaBeans by Writing Your Own Java Code

Including a JavaBean and Custom Controls