Monday, January 4, 2010

Trick or Treat, LayoutFocusTraversalPolicy

Have you ever met such focus traversal issue in Java UI applications? By default, LayoutFocusTraversalPolicy is used to determine the sequence of focus traversal. As stated in Java documents, it sorts components based on their size, position, orientation etc.. However, when two identical components are laid at the same position, there will be a focus traversal issue.

A demo is better than a thousand words. Copy the code to a file and compile and run it.

package tsul.example;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.lang.reflect.Field;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class FocusTraversal extends JPanel {

    JButton b1 = new JButton("Columns");
    JButton b2 = new JButton("Rows");
    JTextArea content = new JTextArea("Textarea");
     * This is the main pane
    JScrollPane pane = new JScrollPane();

    public FocusTraversal() {
        super(new BorderLayout());

        // Try to add a duplicate component to pane's header. In such a case,
        // the default
        // LayoutFocusTraversalPolicy won't work properly as expected. The
        // result is we
        // cannot move focus from Columns to Rows by tabling.
        try {
            Field f = Container.class.getDeclaredField("component");
            // bypass default security check
            ArrayList comp = (ArrayList) f.get(pane.getColumnHeader());
        } catch (Exception e) {
        setPreferredSize(new Dimension(600, 400));
        add(pane, BorderLayout.CENTER);

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                frame = new JFrame("Focus Traversal Example");
                FocusTraversal panel = new FocusTraversal();

    static JFrame frame;