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 FocusTraversal.java 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());

        pane.setColumnHeaderView(b1);
        // 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
            f.setAccessible(true);
            ArrayList comp = (ArrayList) f.get(pane.getColumnHeader());
            comp.add(b1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        pane.setRowHeaderView(b2);
        pane.setViewportView(content);
        setPreferredSize(new Dimension(600, 400));
        add(pane, BorderLayout.CENTER);
    }

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

            @Override
            public void run() {
                frame = new JFrame("Focus Traversal Example");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                FocusTraversal panel = new FocusTraversal();
                frame.getContentPane().add(panel);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    static JFrame frame;

}

No comments:

Post a Comment

Please post your comment here. ;)