Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Focus Transversal Support

The JFC contains a default focus manager that will determine which component will obtain the focus when a focus transversal request is made. The default focus manager will determine the next component to receive input focus and transfer focus to that component. The default search algorithm is a top-to-bottom, left-to-right search. The search algorithm can be modified by altering the focus manager itself, or by having the components in the view contain hints for the focus manager. The focus manager is discussed in detain in Chapter 26, “Focus Managers.”

The JComponent class contains the setNextFocusableComponent method to set the component that should receive the input focus after the component whose property was set. This method takes a single component as an argument that is stored in a client property in the JComponent. Calling this method with a null parameter will clear the property. The focus manager calls the getNextFocusableComponent method to retrieve the component that should receive the focus next. If this method returns null, the focus manager determines the next component to receive focus by itself. This is a public method, so it can be called by classes other than the focus manager if required.

The default focus transversal order for the JTextField instances shown in Figure 3.2 can be reversed by using the code shown next. This example demonstrates that care must be taken when setting the next focus component. It is very easy to create a focus transversal order that isn’t expected by the user. This will make your application difficult to use.

firstNameText.setNextFocusableComponent( ageText );
lastNameText.setNextFocusableComponent( firstNameText );
ageText.setNextFocusableComponent( lastNameText );


Caution:  
A bug in the DefaultFocusManager that is used by the JFC throws an array out-of-bounds exception when a next focus component has been set by using the setNextFocusableComponent method.

The isFocusTraversable method can be called to determine if a JComponent instance can acquire the input focus. Subclasses may need to override this method to return true if they can obtain the input focus. The default implementation in the JComponent class will only return true if a registered KeyStroke action has the WHEN_FOCUSED condition. Subclasses may also need to override the isFocusCycleRoot method. When this method returns true, it tells the focus manager that this component contains its own focus cycle. This will keep the focus cycling within this component and not departing to other components on the next focus component event. The default implementation of this method returns false. A JComponent instance can manage its own focus by returning true from the isManagingFocus method. When this is the case, the focus manager will ignore focus transversal events in that component. It is the responsibility of the component to handle these events properly. The default implementation of this method in the JComponent class returns false.

A component can ask the focus manager for the input focus or to give focus to a component by calling the requestFocus method. However, the component must have the requestFocuesEnabled property set to true for this method to succeed. The setRequestFocusEnabled method can be used to enable or disable the requestFocusEnabled property. Its default value is true, allowing the focus to be requested for the component. The current state of this property can be queried by using the isRequestFocusEnabled method. The requestFocusEnabled property is not bound. It should be noted that setting this property to false does not disable the component from receiving the focus, only from receiving it programmatically via the requestFocus method. As was shown earlier, the isFocusTraversable method can be used to disable a component from receiving the input focus. The hasFocus method can be used to determine if a component currently has the input focus. The requestDefaultFocus can be called on a container to give the input focus to the component that is the default for that container. For example, the default focus component on a dialog box can be the OK button.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.