• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: FocusTraversal
title: Focusの移動
tags: [FocusTraversalPolicy, Focus]
author: aterai
pubdate: 2004-04-26T12:45:56+09:00
description: FocusTraversalPolicyを使って、KBD{Tab}キーなどによるフォーカスの移動を制御します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTNE9BAwqI/AAAAAAAAAaM/57d2rzX7ixk/s800/FocusTraversal.png
---
* 概要 [#summary]
`FocusTraversalPolicy`を使って、KBD{Tab}キーなどによるフォーカスの移動を制御します。

#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTNE9BAwqI/AAAAAAAAAaM/57d2rzX7ixk/s800/FocusTraversal.png)

* サンプルコード [#sourcecode]
#code(link){{
final JButton nb = new JButton("NORTH");
final JButton sb = new JButton("SOUTH");
final JButton wb = new JButton("WEST");
final JButton eb = new JButton("EAST");
JButton nb = new JButton("NORTH");
JButton sb = new JButton("SOUTH");
JButton wb = new JButton("WEST");
JButton eb = new JButton("EAST");
add(new JScrollPane(textarea));
add(nb, BorderLayout.NORTH);
add(sb, BorderLayout.SOUTH);
add(wb, BorderLayout.WEST);
add(eb, BorderLayout.EAST);

FocusTraversalPolicy policy = new FocusTraversalPolicy() {
  //private final List<Component> order = Arrays.asList(
  //    new Component[] {eb, wb, sb, nb});
  private final List<? extends Component> order = Arrays.asList(eb, wb, sb, nb);
  @Override public Component getFirstComponent(Container focusCycleRoot) {
    return order.get(0);
  }

  @Override public Component getLastComponent(Container focusCycleRoot) {
    return order.get(order.size() - 1);
  }

  @Override public Component getComponentAfter(
      Container focusCycleRoot, Component aComponent) {
    int i = order.indexOf(aComponent);
    return order.get((i + 1) % order.size());
  }

  @Override public Component getComponentBefore(
      Container focusCycleRoot, Component aComponent) {
    int i = order.indexOf(aComponent);
    return order.get((i - 1 + order.size()) % order.size());
  }

  @Override public Component getDefaultComponent(Container focusCycleRoot) {
    return order.get(0);
  }
};
frame.setFocusTraversalPolicy(policy);
//setFocusTraversalPolicyProvider(true);
//setFocusTraversalPolicy(policy);
// setFocusTraversalPolicyProvider(true);
// setFocusTraversalPolicy(policy);
}}

* 解説 [#explanation]
上記のサンプルでは、`FocusTraversalPolicy`を使用することで、キー入力によるフォーカスの移動を制御しています。また、`JRadioButton`で以下のような`FocusTraversalPolicy`に切り替えることができます。
上記のサンプルでは、`FocusTraversalPolicy`を使用してキー入力によるフォーカスの移動を制御しています。また、`JRadioButton`で以下のような`FocusTraversalPolicy`に切り替えが可能です。

- `Default`
-- `JPanel`のデフォルトは、`null`
-- 実際のキー入力によるフォーカスの移動には、このパネルの親(`JFrame`)に設定されている`FocusTraversalPolicy`が使用される

-- 実際のキー入力によるフォーカスの移動にはこのパネルの親(`JFrame`)に設定されている`FocusTraversalPolicy`を使用
- `Custom`
-- [https://www.ibm.com/developerworks/jp/java/library/j-mer07153/ Merlinの魔術: フォーカス、フォーカス、フォーカス]からの引用
-- KBD{Tab}キーを押していくと、東西南北の順でボタンのフォーカスが移動(KBD{Shift+Tab}キーでは逆順)
-- `4`つの`JButton`以外には、KBD{Tab}キーでフォーカスは移動しない

-- KBD{Tab}キーを押していくと東西南北の順でボタンのフォーカスが移動(KBD{Shift+Tab}キーでは逆順)
-- `4`つの`JButton`以外にはKBD{Tab}キーでフォーカスは移動しない
- `Layout`
-- 以下のように`LayoutFocusTraversalPolicy`(`LayoutFocusTraversalPolicy`は`Swing`のデフォルト、`AWT`のデフォルトは`DefaultFocusTraversalPolicy`)の`accept`メソッドをオーバーライドして、中央の`JTextArea`(通常、`JTextArea`などから次のコンポーネントにフォーカス移動する場合は、KBD{Ctrl+Tab})が編集不可の場合は、これにKBD{Tab}キーなどでフォーカスが移動しないように設定している

-- 以下のように`LayoutFocusTraversalPolicy`(`LayoutFocusTraversalPolicy`は`Swing`のデフォルト、`AWT`のデフォルトは`DefaultFocusTraversalPolicy`)の`accept`メソッドをオーバーライドして中央の`JTextArea`(通常`JTextArea`などから次のコンポーネントにフォーカス移動する場合はKBD{Ctrl+Tab})が編集不可の場合は、これにKBD{Tab}キーなどでフォーカスが移動しないように設定
#code{{
frame.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
  @Override protected boolean accept(Component c) {
    if (c instanceof JTextComponent) {
      return ((JTextComponent) c).isEditable();
    } else {
      return super.accept(c);
    }
  }
};
}}

* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/java/awt/doc-files/FocusSpec.html AWTフォーカス・サブシステム]
- [https://www.ibm.com/developerworks/jp/java/library/j-mer07153/ Merlinの魔術: フォーカス、フォーカス、フォーカス]
- [[Windowを開いたときのフォーカスを指定>Swing/DefaultFocus]]
- [[FocusTraversalKeysに矢印キーを追加してフォーカス移動>Swing/FocusTraversalKeys]]

* コメント [#comment]
#comment
#comment