It is actually somewhat rare to actually need a FocusTraversalPolicy because the default tab ordering is the order the components were added to the container.
If that doesn't suit your needs though then you indeed need a FocusTraversalPolicy. It is an abstract class and the abstract methods to override seem to be pretty straightforward, and they have good JavaDoc. Do you have a specific question?
Here is a ridiculous example that may get you going in the right direction. This is a ridiculous example because the same behavior occurs just by adding userNameField to the container before passwordField. However, it does demonstrate how to use it:
public class AuthFormFocusPolicy extends FocusTraversalPolicy {
private JTextField userNameField;
private JTextField passwordField;
public AuthFormFocusPolicy() {
this.userNameField = new JTextField();
this.passwordField = new JTextField();
}
public Component getInitialComponent(Window window) {
return super.getInitialComponent(window);
}
public Component getDefaultComponent(Container focusCycleRoot) {
return this.userNameField;
}
public Component getComponentAfter(Container focusCycleRoot, Component component) {
Component nextComponent;
if (component == this.userNameField) {
nextComponent = this.passwordField;
} else {
nextComponent = this.userNameField;
}
return nextComponent;
}
public Component getComponentBefore(Container focusCycleRoot, Component component) {
Component previousComponent;
if (component == this.userNameField) {
previousComponent = this.passwordField;
} else {
previousComponent = this.userNameField;
}
return previousComponent;
}
public Component getFirstComponent(Container focusCycleRoot) {
return this.userNameField;
}
public Component getLastComponent(Container focusCycleRoot) {
return this.passwordField;
}
}