Reputation: 1256
After solving the issue of the background image, I've really come into a no go with trying to change the Focus policy.
I create the class and override the methods like this:
package tab;
import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.util.Vector;
public class MyOFocusTraversalPolicy extends FocusTraversalPolicy{
Vector<Component> order;
public MyOFocusTraversalPolicy(Vector<Component> order) {
this.order = new Vector<Component>(order.size());
this.order.addAll(order);
}
@Override
public Component getComponentAfter(Container focusCycleRoot,
Component aComponent) {
int idx = (order.indexOf(aComponent) + 1);// % order.size();
System.out.println("Estoy en el getComponentAfter, numero: "+idx);
System.out.println("El tamaño del vector es de:"+order.size());
return order.get(idx);
}
@Override
public Component getComponentBefore(Container focusCycleRoot,
Component aComponent) {
int idx = order.indexOf(aComponent) - 1;
if (idx == 0) {
idx = order.size() - 1;
}
return order.get(idx);
}
@Override
public Component getDefaultComponent(Container focusCycleRoot) {
return order.get(0);
}
@Override
public Component getLastComponent(Container focusCycleRoot) {
return order.lastElement();
}
@Override
public Component getFirstComponent(Container focusCycleRoot) {
return order.get(1);
}
}
And I make the vector with all the components in the tab and declare a new Object of MyOFocusTraversalPolicy, like this:
Vector<Component> order = new Vector<Component>(14);
order.add(rBtnHombre);
order.add(rBtnMujer);
order.add(spinEdad);
order.add(slider);
order.add(txtUser);
order.add(txtPass);
order.add(txtConfirmPass);
order.add(txtNombre);
order.add(txtApellidos);
order.add(cBoxProvincias);
order.add(cBoxTipoCalle);
order.add(txtDireccion);
order.add(txtCP);
order.add(txtMail);
newPolicyDatos = new MyOFocusTraversalPolicy(order);
datos.setFocusTraversalPolicy(newPolicyDatos);
Yet it has no effect.
May the problem be because "datos" is one of three tabs that belong to a JTabbedPane, for anything else I've ran out of ideas, and I can't find no specific information about FocusTraversalPolicy on tabs.
I have set a print in the getComponentAfter and it doesn't show when tabbing through the components
Hope someone has tried this before and can give me a hand
Upvotes: 0
Views: 340
Reputation: 93
I know this is way overdue, but I've been trying something similar recently and wondered why the traverse doesn't work like I set it.
The missing line for me was:
myJTabbedPane.add(outerMostJPanel);
outerMostJPanel.setFocusCycleRoot(true); // magic-line
outerMostJPanel.setFocusTraversalPolicy(new MyFocusTraversalPolcy(...));
Hope this helps.
Upvotes: 2