import java.awt.FocusTraversalPolicy;
this.setFocusTraversalPolicy( new CustomTraversalPolicy ( this.getFocusTraversalPolicy() ) );
/**
* inner class to change the default order of tabbing between fields.
* only works in JDK 1.4+
*/
class CustomTraversalPolicy extends FocusTraversalPolicy
{
/**
* The FocusTraversalPolicy normally in effect.
* We use it basically, and override it in a few places.
*/
private FocusTraversalPolicy usual;
/**
* constructor
*
* @param usual The usual FocusTraversalPolicy we are
* overriding. We just make a few changes to
* it.
*/
private CustomTraversalPolicy( FocusTraversalPolicy usual )
{
this.usual = usual;
}
/**
* Returns the Component that should receive the focus after aComponent.
* focusCycleRoot must be a focus cycle root of aComponent.
*
* @param focusCycleRoot a focus cycle root of aComponent
* @param aComponent a (possibly indirect) child of focusCycleRoot, or
* focusCycleRoot itself
* @return the Component that should receive the focus after aComponent, or
* null if no suitable Component can be found
* @throws IllegalArgumentException if focusCycleRoot is not a focus cycle
* root of aComponent, or if either focusCycleRoot or aComponent is
* null
*/
public Component getComponentAfter( Container focusCycleRoot, Component aComponent )
{
if ( aComponent == productNameText )
{
return productDescText;
}
else if ( aComponent == productDescText )
{
return productValueText;
}
else if ( aComponent == productValueText )
{
return chopRowsSpinner.getEditor().getComponent(0);
}
else
{
return usual.getComponentAfter ( focusCycleRoot, aComponent );
}
}
/**
* Returns the Component that should receive the focus before aComponent.
* focusCycleRoot must be a focus cycle root of aComponent.
*
* @param focusCycleRoot a focus cycle root of aComponent
* @param aComponent a (possibly indirect) child of focusCycleRoot, or
* focusCycleRoot itself
* @return the Component that should receive the focus before aComponent,
* or null if no suitable Component can be found
* @throws IllegalArgumentException if focusCycleRoot is not a focus cycle
* root of aComponent, or if either focusCycleRoot or aComponent is
* null
*/
public Component getComponentBefore( Container focusCycleRoot, Component aComponent )
{
if ( aComponent == productDesc )
{
return productNameText;
}
else if ( aComponent == productValueText )
{
return productDescText;
}
else if ( aComponent == chopRowsSpinner.getEditor().getComponent(0) )
{
return productValueText;
}
else
{
return usual.getComponentBefore ( focusCycleRoot, aComponent );
}
}
/**
* Returns the default Component to focus. This Component will be the first
* to receive focus when traversing down into a new focus traversal cycle
* rooted at focusCycleRoot.
*
* @param focusCycleRoot the focus cycle root whose default Component is to
* be returned
* @return the default Component in the traversal cycle when focusCycleRoot
* is the focus cycle root, or null if no suitable Component can
* be found
* @throws IllegalArgumentException if focusCycleRoot is null
*/
public Component getDefaultComponent( Container focusCycleRoot )
{
return usual.getDefaultComponent ( focusCycleRoot );
}
/**
* Returns the first Component in the traversal cycle. This method is used
* to determine the next Component to focus when traversal wraps in the
* forward direction.
*
* @param focusCycleRoot the focus cycle root whose first Component is to
* be returned
* @return the first Component in the traversal cycle when focusCycleRoot
* is the focus cycle root, or null if no suitable Component can be
* found
* @throws IllegalArgumentException if focusCycleRoot is null
*/
public Component getFirstComponent( Container focusCycleRoot )
{
return usual.getFirstComponent ( focusCycleRoot );
}
/**
* Returns the Component that should receive the focus when a Window is
* made visible for the first time. Once the Window has been made visible
* by a call to <code>show()</code> or <code>setVisible( true )</code>, the
* initial Component will not be used again. Instead, if the Window loses
* and subsequently regains focus, or is made invisible or undisplayable
* and subsequently made visible and displayable, the Window's most
* recently focused Component will become the focus owner. The default
* implementation of this method returns the default Component.
*
* @param window the Window whose initial Component is to be returned
* @return the Component that should receive the focus when window is made
* visible for the first time, or null if no suitable Component can
* be found
* @see #getDefaultComponent
* @see Window#getMostRecentFocusOwner
* @throws IllegalArgumentException if window is null
*/
public Component getInitialComponent( Window window )
{
return usual.getInitialComponent ( window );
}
/**
* Returns the last Component in the traversal cycle. This method is used
* to determine the next Component to focus when traversal wraps in the
* reverse direction.
*
* @param focusCycleRoot the focus cycle root whose last Component is to be
* returned
* @return the last Component in the traversal cycle when focusCycleRoot is
* the focus cycle root, or null if no suitable Component can be
* found
* @throws IllegalArgumentException if focusCycleRoot is null
*/
public Component getLastComponent( Container focusCycleRoot )
{
return usual.getFirstComponent ( focusCycleRoot );
}
}