Java Swing FocusTraversalPolicy问题 [英] Java Swing FocusTraversalPolicy Issue
问题描述
我目前遇到了一个执行 FocusTraversalPolicy
的问题。
我想为 Tab
/ Shift + Tab
键盘快捷键允许导航上下/左右。这样可以在填写表单时实现更简单的导航。
策略应该只会影响 我已经使用以下类实现了策略: 然后在构造函数中用下面的方法实例化类(例如main 然而,当我尝试浏览元素时,没有任何变化,以前的策略仍在地点。我已经试过通过Java教程: 任何帮助将不胜感激。 我不确定你为什么甚至在你的情况下扩展 你可以看看 它有一个很好的功能: I'm currently having an issue with implementing a I would like for the The policy should only impact all elements in the I have implemented the policy using the following class: I then instantiate the class with the following inside the constructor of my view (e.g. main However, when I try to Tab through the elements, nothing has changed and the previous policy is still in place. I've already tried going through the Java tutorial: link Any help would be much appreciated. I am not sure why you are even extending the However, you need to set You can look into the It has a nice function: 这篇关于Java Swing FocusTraversalPolicy问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! JFrame $的
mainPanel
中的所有元素c $ c $。
public class DefaultViewFocusTraversalPolicy
extends FocusTraversalPolicy
{
ArrayList< Component>订购;
public DefaultViewFocusTraversalPolicy(ArrayList< Component> order)
{
this.order = order;
$ b @Override
public Component getComponentAfter(Container aContainer,Component aComponent)
{
int index =(order.indexOf(aComponent)+ 1 )%order.size();
Component after = order.get(index); ()< order.size()&&!(after.isEnabled()&& after.isVisible())
(
index ++;
after = order.get(index);
}
之后返回;
$ b @Override
public Component getComponentBefore(Container aContainer,Component aComponent)
{
int index = order.indexOf(aComponent) - 1;
if(index <0)
{
index = order.size() - 1;
}
Component before = order.get(index); (index> = 0&&& ;;(EventsisEnabled()&& electisVisible())
(
index-;
before = order.get(index);
}
之前返回;
$ b @Override
public Component getFirstComponent(Container aContainer)
{
int index = 0;
Component first = order.get(index); (index< order.size()&!(first.isEnabled()&& first.isVisible()))
{
index ++;
first = order.get(index);
}
首先返回;
$ b @Override
public Component getLastComponent(Container aContainer)
{
int index = order.size() - 1;
Component last = order.get(index); (index> = 0&&!(last.isEnabled()&& last.isVisible()))
(
index-;
last = order.get(index);
}
最后返回;
}
@Override
public Component getDefaultComponent(Container aContainer)
{
return getFirstComponent(aContainer);
$ b $ p
$ b JFrame
):
ArrayList< Component> order = new ArrayList<>();
order.add(this.ecmID);
order.add(this.modeID);
// ...
DefaultViewFocusTraversalPolicy policy =
DefaultViewFocusTraversalPolicy(order);
this.mainPanel.setFocusTraversalPolicy(policy);
FocusTraversalPolicy
,而默认 FocusTraversalPolicy
应该为你做这项工作。但是,您需要设置 this.mainPanel.setFocusCycleRoot(true)
: 它设置此Container是否是焦点遍历循环的根。一旦焦点进入遍历循环,通常它不能通过焦点遍历离开它,除非按下上下键中的一个。正常的遍历仅限于这个Container,并且所有这个Container的后代不是次要的重点循环的根源。
ContainerOrderFocusTraversalPolicy
$ b
Container.getComponents()
boolean accept(Component aComponent)
:确定组件是否是新焦点所有者的可接受选项。只需要通过扩展这个类来覆盖这个函数,你不需要关注 Container
中相应的组件列表。 不需要重写所有的函数并实现它们。 FocusTraversalPolicy
. Tab
/Shift+Tab
keyboard shortcuts to allow for navigating top-bottom/left-right. This will allow for simpler navigation when filling in a form. mainPanel
of my JFrame
.public class DefaultViewFocusTraversalPolicy
extends FocusTraversalPolicy
{
ArrayList<Component> order;
public DefaultViewFocusTraversalPolicy(ArrayList<Component> order)
{
this.order = order;
}
@Override
public Component getComponentAfter(Container aContainer, Component aComponent)
{
int index = (order.indexOf(aComponent) + 1) % order.size();
Component after = order.get(index);
while (index < order.size() && !(after.isEnabled() && after.isVisible()))
{
index++;
after = order.get(index);
}
return after;
}
@Override
public Component getComponentBefore(Container aContainer, Component aComponent)
{
int index = order.indexOf(aComponent) - 1;
if (index < 0)
{
index = order.size() - 1;
}
Component before = order.get(index);
while (index >= 0 && !(before.isEnabled() && before.isVisible()))
{
index--;
before = order.get(index);
}
return before;
}
@Override
public Component getFirstComponent(Container aContainer)
{
int index = 0;
Component first = order.get(index);
while (index < order.size() && !(first.isEnabled() && first.isVisible()))
{
index++;
first = order.get(index);
}
return first;
}
@Override
public Component getLastComponent(Container aContainer)
{
int index = order.size() - 1;
Component last = order.get(index);
while (index >= 0 && !(last.isEnabled() && last.isVisible()))
{
index--;
last = order.get(index);
}
return last;
}
@Override
public Component getDefaultComponent(Container aContainer)
{
return getFirstComponent(aContainer);
}
}
JFrame
):ArrayList<Component> order = new ArrayList<>();
order.add(this.ecmID);
order.add(this.modeID);
// ...
DefaultViewFocusTraversalPolicy policy =
new DefaultViewFocusTraversalPolicy(order);
this.mainPanel.setFocusTraversalPolicy(policy);
FocusTraversalPolicy
for your case while default FocusTraversalPolicy
should do the job for you. this.mainPanel.setFocusCycleRoot(true)
: which sets whether this Container is the root of a focus traversal cycle. Once focus enters a traversal cycle, typically it cannot leave it via focus traversal unless one of the up- or down-cycle keys is pressed. Normal traversal is limited to this Container, and all of this Container's descendants that are not descendants of inferior focus cycle roots.ContainerOrderFocusTraversalPolicy
:
Container.getComponents()
boolean accept(Component aComponent)
: Determines whether a Component is an acceptable choice as the new focus owner. just override this function by extending this class, with your corresponding component list you don't want to be focused of the Container
. No need to override all of the function and implement them.