Java Swing FocusTraversalPolicy 问题

标签 java swing focus

我目前在实现 FocusTraversalPolicy 时遇到问题。

我希望 Tab/Shift+Tab 键盘快捷键允许上下/左右导航。这将允许在填写表单时进行更简单的导航。

该策略应该只影响我的 JFramemainPanel 中的所有元素。

我已经使用以下类实现了政策:

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

然后我在 View 的构造函数中使用以下内容实例化该类(例如 main JFrame):

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

但是,当我尝试通过 Tab 键浏览元素时,没有任何改变,之前的策略仍然有效。我已经尝试过 Java 教程:link

如有任何帮助,我们将不胜感激。

最佳答案

我不确定你为什么要为你的情况扩展 FocusTraversalPolicy 而默认的 FocusTraversalPolicy 应该为你完成这项工作。

但是,您需要设置this.mainPanel.setFocusCycleRoot(true):设置此Container是否为焦点遍历循环的根。一旦焦点进入遍历循环,通常它不能通过焦点遍历离开它,除非按下向上或向下循环键之一。正常遍历仅限于此容器,以及此容器的所有非劣质焦点循环根的后代。

您可以查看 ContainerOrderFocusTraversalPolicy :

  1. 根据Container.getComponents()返回的顺序确定遍历顺序
  2. 从特定的焦点循环根开始,策略对组件层次结构进行预排序遍历。

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

关于Java Swing FocusTraversalPolicy 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20200589/

相关文章:

java - 工作恢复时 quartz 故障

java - 在方法错误中关闭 Action 监听器上的 JFrame

java - 如何通过 Main - NullpointerException 错误更改进度条的值

javascript - 从 AG-Grid 中移除焦点

javascript - 如何触发 (window).focus 页面加载?

java - 如何将 Java 放入 GameMaker Studio?

java - 如何使用 Maven 在目标文件夹中创建新目录

java - 当焦点离开我的 JTable 时,结合 'Excel-like' 行为并更新模型

java - 在 super 调用Java中调用父的内部类

java - 如何调整 JLabel ImageIcon 的大小?