Focustraversalpolicy is a new feature introduced after jdk1.4 to customize the focus sequence.
Let's take an example:
Public class myiframe extends jinternalframe {
......
Protected void setcompfocus (component [] compfocus ){
Getcontentpane (). setfocusable (false );
Setfocustraversalpolicy (New
Custom_focustraversalpolicy (compfocus ));
}
Class custom_focustraversalpolicy extends focustraversalpolicy {
Component [] compfocus;
List list;
Public custom_focustraversalpolicy (){
}
Public custom_focustraversalpolicy (component [] compfocus ){
This. compfocus = compfocus;
List = arrays. aslist (compfocus );
}
Public component getfirstcomponent (container focuscycleroot ){
Return compfocus [0];
}
Public component getlastcomponent (container focuscycleroot ){
Return compfocus [compfocus. Length-1];
}
Public component getcomponentafter (
Container focuscycleroot, component acomponent ){
Int Index = List. indexof (acomponent );
If (compfocus [(index + 1) % compfocus. Length]. isenabled ()){
Return compfocus [(index + 1) % compfocus. Length];
} Else {
Return
Getcomponentafter (focuscycleroot, compfocus [(index +
1) % compfocus. Length]);
}
}
Public component getcomponentbefore (
Container focuscycleroot,
Component acomponent ){
Int Index = List. indexof (acomponent );
If (compfocus [(index-1 + compfocus. Length) %
Compfocus. Length]. isenabled ()){
Return compfocus [(index-1 + compfocus. Length) %
Compfocus. Length];
} Else {
Return
Getcomponentbefore (focuscycleroot, compfocus [(Index
-1 + compfocus. Length) % compfocus. Length]);
}
}
Public component getdefaultcomponent (container
Focuscycleroot ){
Return compfocus [0];
}
}
}
In this case, the components defined on myiframe are: A, B, C, and D. Requirement: After the screen is started, focus on B. Press "tab, focus is in the order of B-> D-> C-> A, and disable controls are skipped. Call the setcompfocus method with the array {B, D, C, A} as the parameter.