Tabbing Between Components

Tabbing Between Components

With Java 1.1, the Tab key will cause the focus to step through those Components in a Container which can accept user input. Components such as Labels, which do not expect input, are skipped. The order of the tab sequence is generally the order in which the Components were passed to the Container’s add method. By extending a Component’s class you can provide control of when an instance can accept the focus, or whether it does so at all. In your Component class, add a variable as follows:

     private boolean tabbable;

Then add a pair of methods:

     public boolean isFocusTraversable() {        return tabbable;    }    public void setFocusTraversable(boolean tab_ok) {        tabbable = tab_ok;    }

The first of these is used when the Tab key is hit, to determine whether the Component can accept the focus, so you must give it the expected name, isFocusTraversable.


Share the Post: