我还在学习自定义遍历政策,这确实让我误解了几个小时。为什么它不会遍历文本区域?它只跳过文本字段。
所以这些是我添加的一些组件。
Vector<Component> order = new Vector<Component>(11);
order.add(txtfldBRN);
order.add(txtfldName);
order.add(cmbxDOBirth);
order.add(txtfldBirthPlace);
order.add(txtfldMother);
order.add(txtfldFather);
order.add(cmbxDOBaptism);
order.add(txtfldMinister);
order.add(txtAreaSponsors);
order.add(txtAreaComments);
order.add(btnSave);
static MyOwnFocusTraversalPolicy newPolicy = new MyOwnFocusTraversalPolicy(order);
this.setFocusTraversalPolicy(newPolicy);
}
public static class MyOwnFocusTraversalPolicy extends FocusTraversalPolicy {
Vector<Component> order;
public MyOwnFocusTraversalPolicy(Vector<Component> order) {
System.out.println(order.size());
this.order = new Vector<Component>(order.size());
this.order.addAll(order);
}
public Component getComponentAfter(Container focusCycleRoot,
Component aComponent) {
int idx = (order.indexOf(aComponent) + 1) % order.size();
return order.get(idx);
}
public Component getComponentBefore(Container focusCycleRoot,
Component aComponent) {
int idx = order.indexOf(aComponent) - 1;
if (idx < 0) {
idx = order.size() - 1;
}
System.out.println("Before: "+idx);
return order.get(idx);
}
public Component getDefaultComponent(Container focusCycleRoot) {
return order.get(0);
}
public Component getLastComponent(Container focusCycleRoot) {
return order.lastElement();
}
public Component getFirstComponent(Container focusCycleRoot) {
return order.get(0);
}
}