[英]Setting personalized FocusTraversalPolicy on tab in JTabbedPane
After solving the issue of the background image, I've really come into a no go with trying to change the Focus policy. 解决了背景图片问题后,我真的尝试更改Focus策略。
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: 然后使用选项卡中的所有组件制作矢量,并声明一个新的MyOFocusTraversalPolicy对象,如下所示:
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. 可能是因为“ datos”是属于JTabbedPane的三个选项卡之一,对于我用尽的其他任何想法,我都无法在选项卡上找到有关FocusTraversalPolicy的特定信息。
I have set a print in the getComponentAfter and it doesn't show when tabbing through the components 我已经在getComponentAfter中设置了打印,并且在浏览组件时不显示
Hope someone has tried this before and can give me a hand 希望有人尝试过这个可以给我帮助
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. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.