Java Swing FocusTraversalPolicy问题 [英] Java Swing FocusTraversalPolicy Issue

查看:188
本文介绍了Java Swing FocusTraversalPolicy问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到了一个执行 FocusTraversalPolicy 的问题。



我想为 Tab / Shift + Tab 键盘快捷键允许导航上下/左右。这样可以在填写表单时实现更简单的导航。



策略应该只会影响 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

然后在构造函数中用下面的方法实例化类(例如main JFrame ):

  ArrayList< Component> order = new ArrayList<>(); 
order.add(this.ecmID);
order.add(this.modeID);
// ...
DefaultViewFocusTraversalPolicy policy =
DefaultViewFocusTraversalPolicy(order);
this.mainPanel.setFocusTraversalPolicy(policy);

然而,当我尝试浏览元素时,没有任何变化,以前的策略仍在地点。我已经试过通过Java教程:

任何帮助将不胜感激。

解决方案

我不确定你为什么甚至在你的情况下扩展 FocusTraversalPolicy ,而默认 FocusTraversalPolicy 应该为你做这项工作。但是,您需要设置 this.mainPanel.setFocusCycleRoot(true) 它设置此Container是否是焦点遍历循环的根。一旦焦点进入遍历循环,通常它不能通过焦点遍历离开它,除非按下上下键中的一个。正常的遍历仅限于这个Container,并且所有这个Container的后代不是次要的重点循环的根源。

你可以看看 ContainerOrderFocusTraversalPolicy $ b


  1. 决定遍历顺序: Container.getComponents()

  2. 从特定的焦点循环根目录,策略对组件层次结构进行预先遍历。

它有一个很好的功能: boolean accept(Component aComponent) :确定组件是否是新焦点所有者的可接受选项。只需要通过扩展这个类来覆盖这个函数,你不需要关注 Container 中相应的组件列表。 不需要重写所有的函数并实现它们。


I'm currently having an issue with implementing a FocusTraversalPolicy.

I would like for the Tab/Shift+Tab keyboard shortcuts to allow for navigating top-bottom/left-right. This will allow for simpler navigation when filling in a form.

The policy should only impact all elements in the mainPanel of my JFrame.

I have implemented the policy using the following class:

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);
   }
}

I then instantiate the class with the following inside the constructor of my view (e.g. main JFrame):

ArrayList<Component> order = new ArrayList<>();
order.add(this.ecmID);
order.add(this.modeID);
// ...
DefaultViewFocusTraversalPolicy policy =
        new DefaultViewFocusTraversalPolicy(order);
this.mainPanel.setFocusTraversalPolicy(policy);

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 FocusTraversalPolicy for your case while default FocusTraversalPolicy should do the job for you.

However, you need to set 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.

You can look into the ContainerOrderFocusTraversalPolicy:

  1. determines traversal order based on the order:returned by Container.getComponents()
  2. From a particular focus cycle root, the policy makes a pre-order traversal of the Component hierarchy .

It has a nice function: 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.

这篇关于Java Swing FocusTraversalPolicy问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆