Exclude Components from Tab Order

Today I set out to accomplish something that appeared to be quite simple. However, it took me quite a few hours and lots of reading to find a solution. I do hope that this will be useful to other developers!

I have been working a JPanel form with a lot of different components on it, including JTextFields, JDateChoosers and some custom Swing components. And today I wanted to exclude some of the controls from the tab order. The controls I wanted to exclude were enabled but set as not editable, to allow the user to copy the values as required.

The first thing I tried was setting the components as not focusable. However, that meant that I couldn’t select the text with the mouse pointer anymore. So back to the drawing board.

I moved on to reading about the FocusTraversalPolicy mechanism (which I have never used before). And after much trial and error, I found this very simple solution. Just extend the class LayoutFocusTraversalPolicy, overriding the accept method as required. Here is the newly created class:

public class ExclusionFocusTraversalPolicy 
        extends LayoutFocusTraversalPolicy {

    private ArrayList components = new ArrayList();

    public void addExcludedComponent(Component component) {
        components.add(component);
    }

    @Override
    protected boolean accept(Component aComponent) {
        if (components.contains(aComponent)) {
            return false;
        }
        return super.accept(aComponent);
    }
}

And then in the form after adding the components call:

ExclusionFocusTraversalPolicy policy = new ExclusionFocusTraversalPolicy();
// add all the desired components to the exclusion list
policy.addExcludedComponent(textField);
setFocusTraversalPolicy(policy);
setFocusTraversalPolicyProvider(true);

That is it. Now when tabbing through the form, all the components in the exclusion list are skipped!

2 thoughts on “Exclude Components from Tab Order”

  1. Select your JFrame or JPanel or etc and then jField or jButton or etc of the JFrame and go to properties of the JField or JButton and enable the “focusCycleRoot” (this JField or JButton will be selected first in the jFrame or JPanel ) then select the “change order” of the JFrame or JPanel in the Navigator window and “Move up” or “Move down” to change the order.

  2. Thank you providing this solution. I have been searching the net for hours trying to find a solutions since the components that I wanted to include in my tab order existed in different JPanels. Your solution for excluding components from the tab order worked like a charm for me. Thank you so much!

Leave a Comment