下面列出了java.awt.Container#getFocusTraversalPolicy ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Component getFirstComponent(Container aContainer) {
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getFirstComponent(root);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getFirstComponent(root);
}
return null;
}
public Component getComponentAfter(Container aContainer,
Component aComponent)
{
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getComponentAfter(root, aComponent);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getComponentAfter(root, aComponent);
}
return null;
}
public Component getLastComponent(Container aContainer) {
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getLastComponent(root);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getLastComponent(root);
}
return null;
}
public Component getComponentAfter(Container aContainer,
Component aComponent)
{
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getComponentAfter(root, aComponent);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getComponentAfter(root, aComponent);
}
return null;
}
@Override
public void actionPerformed(ActionEvent e) {
int selIndexBefore = getSelectedIndex();
defaultAction.actionPerformed( e );
int selIndexCurrent = getSelectedIndex();
if( selIndexBefore != selIndexCurrent )
return;
if( focusNext && 0 == selIndexCurrent && getModel().getSize() > 1 && getModel().getSize() > getColumnCount() )
return;
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
Container container = kfm.getCurrentFocusCycleRoot();
FocusTraversalPolicy policy = container.getFocusTraversalPolicy();
if( null == policy )
policy = kfm.getDefaultFocusTraversalPolicy();
Component next = focusNext ? policy.getComponentAfter( container, CategoryList.this )
: policy.getComponentBefore( container, CategoryList.this );
if( null != next && next instanceof CategoryButton ) {
clearSelection();
next.requestFocus();
}
}
public Component getComponentAfter(Container aContainer,
Component aComponent)
{
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getComponentAfter(root, aComponent);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getComponentAfter(root, aComponent);
}
return null;
}
public Component getLastComponent(Container aContainer) {
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getLastComponent(root);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getLastComponent(root);
}
return null;
}
public Component getComponentAfter(Container aContainer,
Component aComponent)
{
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getComponentAfter(root, aComponent);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getComponentAfter(root, aComponent);
}
return null;
}
public Component getComponentAfter(Container aContainer,
Component aComponent)
{
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getComponentAfter(root, aComponent);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getComponentAfter(root, aComponent);
}
return null;
}
public Component getFirstComponent(Container aContainer) {
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getFirstComponent(root);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getFirstComponent(root);
}
return null;
}
public Component getFirstComponent(Container aContainer) {
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getFirstComponent(root);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getFirstComponent(root);
}
return null;
}
/**
* Returns the first component.
* @return the first component
* @param aContainer a container
*/
public Component getFirstComponent(Container aContainer) {
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getFirstComponent(root);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getFirstComponent(root);
}
return null;
}
public Component getFirstComponent(Container aContainer) {
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getFirstComponent(root);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getFirstComponent(root);
}
return null;
}
/**
* Returns the last component.
* @return the last component
* @param aContainer a container
*/
public Component getLastComponent(Container aContainer) {
Container root = (aContainer.isFocusCycleRoot())
? aContainer
: aContainer.getFocusCycleRootAncestor();
// Support for mixed 1.4/pre-1.4 focus APIs. If a particular root's
// traversal policy is non-legacy, then honor it.
if (root != null) {
FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
if (policy != gluePolicy) {
return policy.getLastComponent(root);
}
comparator.setComponentOrientation(root.getComponentOrientation());
return layoutPolicy.getLastComponent(root);
}
return null;
}
/**
* Returns a set of all the components that
* can have the keyboard focus.
* <P>My first implementation involved of this concept
* simply involved asking JCompnonents if they were
* focusable, but in the <code>FilledButtonTest</code> this
* resulted in shifting focus to the ContentPane. Although
* it is technically focusable: if I used the tab key
* I did <i>not</i> get this result. So I studied
* the inner workings for Component.transferFocus()
* and ended up with a method that involved
* calls to <code>getFocusCycleRootAncestor()</code>,
* and <code>getFocusTraversalPolicy()</code>.
* <P>(Also credit goes to Werner for originally tipping me off
* towards looking at FocusTraversalPolicies.)
*
* @param currentFocusOwner the current focus owner.
* @return all the JComponents that can receive the focus.
*/
public static Set<Component> getFocusableComponents(Component currentFocusOwner) {
Set<Component> set = new HashSet<>();
set.add(currentFocusOwner);
Container rootAncestor = currentFocusOwner.getFocusCycleRootAncestor();
Component comp = currentFocusOwner;
while (rootAncestor != null &&
!(rootAncestor.isShowing() &&
rootAncestor.isFocusable() &&
rootAncestor.isEnabled())) {
comp = rootAncestor;
rootAncestor = comp.getFocusCycleRootAncestor();
}
if (rootAncestor != null) {
FocusTraversalPolicy policy =
rootAncestor.getFocusTraversalPolicy();
Component toFocus = policy.getComponentAfter(rootAncestor, comp);
while (toFocus != null && !set.contains(toFocus)) {
set.add(toFocus);
toFocus = policy.getComponentAfter(rootAncestor, toFocus);
}
toFocus = policy.getComponentBefore(rootAncestor, comp);
while (toFocus != null && !set.contains(toFocus)) {
set.add(toFocus);
toFocus = policy.getComponentBefore(rootAncestor, toFocus);
}
}
return set;
}
/** Returns a set of all the components that
* can have the keyboard focus.
* <P>My first implementation involved of this concept
* simply involved asking JCompnonents if they were
* focusable, but in the <code>FilledButtonTest</code> this
* resulted in shifting focus to the ContentPane. Although
* it is technically focusable: if I used the tab key
* I did <i>not</i> get this result. So I studied
* the inner workings for Component.transferFocus()
* and ended up with a method that involved
* calls to <code>getFocusCycleRootAncestor()</code>,
* and <code>getFocusTraversalPolicy()</code>.
* <P>(Also credit goes to Werner for originally tipping me off
* towards looking at FocusTraversalPolicies.)
* @param currentFocusOwner the current focus owner.
* @return all the JComponents that can receive the focus.
*/
public static Set<Component> getFocusableComponents(Component currentFocusOwner) {
HashSet<Component> set = new HashSet<Component>();
set.add(currentFocusOwner);
Container rootAncestor = currentFocusOwner.getFocusCycleRootAncestor();
Component comp = currentFocusOwner;
while (rootAncestor != null &&
!(rootAncestor.isShowing() &&
rootAncestor.isFocusable() &&
rootAncestor.isEnabled()))
{
comp = rootAncestor;
rootAncestor = comp.getFocusCycleRootAncestor();
}
if (rootAncestor != null) {
FocusTraversalPolicy policy =
rootAncestor.getFocusTraversalPolicy();
Component toFocus = policy.getComponentAfter(rootAncestor, comp);
while(toFocus!=null && set.contains(toFocus)==false) {
set.add(toFocus);
toFocus = policy.getComponentAfter(rootAncestor, toFocus);
}
toFocus = policy.getComponentBefore(rootAncestor, comp);
while(toFocus!=null && set.contains(toFocus)==false) {
set.add(toFocus);
toFocus = policy.getComponentBefore(rootAncestor, toFocus);
}
}
return set;
}
/**
* Returns the Component that should receive the focus before aComponent.
* aContainer must be a focus cycle root of aComponent or a focus traversal policy provider.
* <p>
* By default, SortingFocusTraversalPolicy implicitly transfers focus down-
* cycle. That is, during normal focus traversal, the Component
* traversed after a focus cycle root will be the focus-cycle-root's
* default Component to focus. This behavior can be disabled using the
* <code>setImplicitDownCycleTraversal</code> method.
* <p>
* If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
* traversal policy provider</a>, the focus is always transferred down-cycle.
*
* @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
* @param aComponent a (possibly indirect) child of aContainer, or
* aContainer itself
* @return the Component that should receive the focus before aComponent,
* or null if no suitable Component can be found
* @throws IllegalArgumentException if aContainer is not a focus cycle
* root of aComponent or a focus traversal policy provider, or if either aContainer or
* aComponent is null
*/
public Component getComponentBefore(Container aContainer, Component aComponent) {
if (aContainer == null || aComponent == null) {
throw new IllegalArgumentException("aContainer and aComponent cannot be null");
}
if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
} else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
}
// See if the component is inside of policy provider.
Container provider = getTopmostProvider(aContainer, aComponent);
if (provider != null) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("### Asking FTP " + provider + " for component after " + aComponent);
}
// FTP knows how to find component after the given. We don't.
FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
Component beforeComp = policy.getComponentBefore(provider, aComponent);
// Null result means that we overstepped the limit of the FTP's cycle.
// In that case we must quit the cycle, otherwise return the component found.
if (beforeComp != null) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("### FTP returned " + beforeComp);
}
return beforeComp;
}
aComponent = provider;
// If the provider is traversable it's returned.
if (accept(aComponent)) {
return aComponent;
}
}
List<Component> cycle = getFocusTraversalCycle(aContainer);
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("### Cycle is " + cycle + ", component is " + aComponent);
}
int index = getComponentIndex(cycle, aComponent);
if (index < 0) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
}
return getLastComponent(aContainer);
}
Component comp;
Component tryComp;
for (index--; index>=0; index--) {
comp = cycle.get(index);
if (comp != aContainer && (tryComp = getComponentDownCycle(comp, BACKWARD_TRAVERSAL)) != null) {
return tryComp;
} else if (accept(comp)) {
return comp;
}
}
if (aContainer.isFocusCycleRoot()) {
this.cachedRoot = aContainer;
this.cachedCycle = cycle;
comp = getLastComponent(aContainer);
this.cachedRoot = null;
this.cachedCycle = null;
return comp;
}
return null;
}
/**
* Returns the Component that should receive the focus before aComponent.
* aContainer must be a focus cycle root of aComponent or a focus traversal policy provider.
* <p>
* By default, SortingFocusTraversalPolicy implicitly transfers focus down-
* cycle. That is, during normal focus traversal, the Component
* traversed after a focus cycle root will be the focus-cycle-root's
* default Component to focus. This behavior can be disabled using the
* <code>setImplicitDownCycleTraversal</code> method.
* <p>
* If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
* traversal policy provider</a>, the focus is always transferred down-cycle.
*
* @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
* @param aComponent a (possibly indirect) child of aContainer, or
* aContainer itself
* @return the Component that should receive the focus before aComponent,
* or null if no suitable Component can be found
* @throws IllegalArgumentException if aContainer is not a focus cycle
* root of aComponent or a focus traversal policy provider, or if either aContainer or
* aComponent is null
*/
public Component getComponentBefore(Container aContainer, Component aComponent) {
if (aContainer == null || aComponent == null) {
throw new IllegalArgumentException("aContainer and aComponent cannot be null");
}
if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
} else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
}
// See if the component is inside of policy provider.
Container provider = getTopmostProvider(aContainer, aComponent);
if (provider != null) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("### Asking FTP " + provider + " for component after " + aComponent);
}
// FTP knows how to find component after the given. We don't.
FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
Component beforeComp = policy.getComponentBefore(provider, aComponent);
// Null result means that we overstepped the limit of the FTP's cycle.
// In that case we must quit the cycle, otherwise return the component found.
if (beforeComp != null) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("### FTP returned " + beforeComp);
}
return beforeComp;
}
aComponent = provider;
// If the provider is traversable it's returned.
if (accept(aComponent)) {
return aComponent;
}
}
List<Component> cycle = getFocusTraversalCycle(aContainer);
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("### Cycle is " + cycle + ", component is " + aComponent);
}
int index = getComponentIndex(cycle, aComponent);
if (index < 0) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
}
return getLastComponent(aContainer);
}
Component comp;
Component tryComp;
for (index--; index>=0; index--) {
comp = cycle.get(index);
if (comp != aContainer && (tryComp = getComponentDownCycle(comp, BACKWARD_TRAVERSAL)) != null) {
return tryComp;
} else if (accept(comp)) {
return comp;
}
}
if (aContainer.isFocusCycleRoot()) {
this.cachedRoot = aContainer;
this.cachedCycle = cycle;
comp = getLastComponent(aContainer);
this.cachedRoot = null;
this.cachedCycle = null;
return comp;
}
return null;
}
/**
* Returns the Component that should receive the focus before aComponent.
* aContainer must be a focus cycle root of aComponent or a focus traversal policy provider.
* <p>
* By default, SortingFocusTraversalPolicy implicitly transfers focus down-
* cycle. That is, during normal focus traversal, the Component
* traversed after a focus cycle root will be the focus-cycle-root's
* default Component to focus. This behavior can be disabled using the
* <code>setImplicitDownCycleTraversal</code> method.
* <p>
* If aContainer is <a href="../../java/awt/doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus
* traversal policy provider</a>, the focus is always transferred down-cycle.
*
* @param aContainer a focus cycle root of aComponent or a focus traversal policy provider
* @param aComponent a (possibly indirect) child of aContainer, or
* aContainer itself
* @return the Component that should receive the focus before aComponent,
* or null if no suitable Component can be found
* @throws IllegalArgumentException if aContainer is not a focus cycle
* root of aComponent or a focus traversal policy provider, or if either aContainer or
* aComponent is null
*/
public Component getComponentBefore(Container aContainer, Component aComponent) {
if (aContainer == null || aComponent == null) {
throw new IllegalArgumentException("aContainer and aComponent cannot be null");
}
if (!aContainer.isFocusTraversalPolicyProvider() && !aContainer.isFocusCycleRoot()) {
throw new IllegalArgumentException("aContainer should be focus cycle root or focus traversal policy provider");
} else if (aContainer.isFocusCycleRoot() && !aComponent.isFocusCycleRoot(aContainer)) {
throw new IllegalArgumentException("aContainer is not a focus cycle root of aComponent");
}
// See if the component is inside of policy provider.
Container provider = getTopmostProvider(aContainer, aComponent);
if (provider != null) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("### Asking FTP " + provider + " for component after " + aComponent);
}
// FTP knows how to find component after the given. We don't.
FocusTraversalPolicy policy = provider.getFocusTraversalPolicy();
Component beforeComp = policy.getComponentBefore(provider, aComponent);
// Null result means that we overstepped the limit of the FTP's cycle.
// In that case we must quit the cycle, otherwise return the component found.
if (beforeComp != null) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("### FTP returned " + beforeComp);
}
return beforeComp;
}
aComponent = provider;
// If the provider is traversable it's returned.
if (accept(aComponent)) {
return aComponent;
}
}
List<Component> cycle = getFocusTraversalCycle(aContainer);
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("### Cycle is " + cycle + ", component is " + aComponent);
}
int index = getComponentIndex(cycle, aComponent);
if (index < 0) {
if (log.isLoggable(PlatformLogger.Level.FINE)) {
log.fine("### Didn't find component " + aComponent + " in a cycle " + aContainer);
}
return getLastComponent(aContainer);
}
Component comp;
Component tryComp;
for (index--; index>=0; index--) {
comp = cycle.get(index);
if (comp != aContainer && (tryComp = getComponentDownCycle(comp, BACKWARD_TRAVERSAL)) != null) {
return tryComp;
} else if (accept(comp)) {
return comp;
}
}
if (aContainer.isFocusCycleRoot()) {
this.cachedRoot = aContainer;
this.cachedCycle = cycle;
comp = getLastComponent(aContainer);
this.cachedRoot = null;
this.cachedCycle = null;
return comp;
}
return null;
}
@Override
public void actionPerformed(ActionEvent e) {
setFocusCycleRoot(false);
try {
Container con = ETable.this.getFocusCycleRootAncestor();
if (con != null) {
/*
Component target = ETable.this;
if (getParent() instanceof JViewport) {
target = getParent().getParent();
if (target == con) {
target = ETable.this;
}
}
*/
EventObject eo = EventQueue.getCurrentEvent();
boolean backward = false;
if (eo instanceof KeyEvent) {
backward =
(((KeyEvent) eo).getModifiers()
& KeyEvent.SHIFT_MASK)
!= 0 && (((KeyEvent) eo).getModifiersEx() &
KeyEvent.SHIFT_DOWN_MASK) != 0;
}
Component c = ETable.this;
Component to;
Container parentWithFTP = null;
do {
FocusTraversalPolicy ftp = con.getFocusTraversalPolicy();
to = backward ? ftp.getComponentBefore(con, c)
: ftp.getComponentAfter(con, c);
if (to == ETable.this) {
to = backward ? ftp.getFirstComponent(con)
: ftp.getLastComponent(con);
}
if (to == ETable.this) {
parentWithFTP = con.getParent();
if (parentWithFTP != null) {
parentWithFTP = parentWithFTP.getFocusCycleRootAncestor();
}
if (parentWithFTP != null) {
c = con;
con = parentWithFTP;
}
}
} while (to == ETable.this && parentWithFTP != null);
if (to != null) {
to.requestFocus();
}
}
} finally {
setFocusCycleRoot(true);
}
}