Swing/ButtonGroupFocusTraversalPolicy のバックアップの現在との差分(No.6)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ButtonGroupFocusTraversalPolicy へ行く。
- 1 (2016-01-18 (月) 00:11:07)
- 2 (2016-01-19 (火) 16:15:30)
- 3 (2016-05-10 (火) 16:19:43)
- 4 (2016-06-17 (金) 19:41:16)
- 5 (2017-01-18 (水) 19:37:27)
- 6 (2017-04-07 (金) 13:51:51)
- 7 (2017-12-05 (火) 18:47:41)
- 8 (2018-02-24 (土) 19:51:30)
- 9 (2018-11-02 (金) 20:08:42)
- 10 (2020-11-03 (火) 00:16:25)
- 11 (2022-08-20 (土) 22:15:25)
- 12 (2022-09-27 (火) 17:25:14)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: ButtonGroupFocusTraversalPolicy title: ButtonGroup内で最初にフォーカスを取得するJRadioButtonを変更する tags: [ButtonGroup, FocusTraversalPolicy, JRadioButton, Focus] author: aterai pubdate: 2016-01-18T00:09:46+09:00 description: Tabキーによるフォーカス移動が発生した場合、ButtonGroup内で現在選択されているJRadioButtonにフォーカスが移動するよう設定します。 image: https://lh3.googleusercontent.com/-r6C4rorBCYw/Vpuu38No8lI/AAAAAAAAOLc/6xa7VoLpsWI/s800-Ic42/ButtonGroupFocusTraversalPolicy.png hreflang: href: http://java-swing-tips.blogspot.com/2016/05/the-jradiobutton-that-is-currently_10.html href: https://java-swing-tips.blogspot.com/2016/05/the-jradiobutton-that-is-currently_10.html lang: en --- * 概要 [#summary] KBD{Tab}キーによるフォーカス移動が発生した場合、`ButtonGroup`内で現在選択されている`JRadioButton`にフォーカスが移動するよう設定します。 #download(https://lh3.googleusercontent.com/-r6C4rorBCYw/Vpuu38No8lI/AAAAAAAAOLc/6xa7VoLpsWI/s800-Ic42/ButtonGroupFocusTraversalPolicy.png) * サンプルコード [#sourcecode] #code(link){{ buttons.setFocusTraversalPolicyProvider(true); buttons.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() { @Override public Component getDefaultComponent(Container focusCycleRoot) { return Stream.of(focusCycleRoot.getComponents()) .filter(c -> ((JRadioButton) c).getModel().equals(selection)) .findFirst().orElse(super.getDefaultComponent(focusCycleRoot)); .filter(c -> ((JRadioButton) c).getModel().equals(selection)) .findFirst().orElse(super.getDefaultComponent(focusCycleRoot)); } }); }} * 解説 [#explanation] - `Default` -- デフォルトの`LayoutFocusTraversalPolicy`では、`JRadioButton`の選択状態には無関係に、レイアウト内のグループ先頭にある`JRadioButton`にフォーカスが移動する -- %%デフォルトの`LayoutFocusTraversalPolicy`では、`JRadioButton`の選択状態には無関係に、レイアウト内のグループ先頭にある`JRadioButton`にフォーカスが移動する%% -- [https://bugs.openjdk.org/browse/JDK-8033699 [JDK-8033699] Incorrect radio button behavior - Java Bug System]で修正済で、デフォルトで以下と同じ動作をするようになった -- `OracleJDK 1.8.0_192`で再発したが`8u311`で再修正済 --- [https://bugs.openjdk.org/browse/JDK-8268519 [JDK-8268519] Fields not reachable anymore by tab-key, because of new tabbing behaviour of radio button groups. - Java Bug System] -- `OpenJDK 11.0.1`では修正されている --- [https://bugs.openjdk.org/browse/JDK-8154043 [JDK-8154043] Fields not reachable anymore by tab-key, because of new tabbing behaviour of radio button groups. - Java Bug System] - `FocusTraversalPolicy` -- `JRadioButton`を配置したパネルに`getDefaultComponent(...)`メソッドをオーバーライドした`FocusTraversalPolicy`を設定し、現在選択されている`JRadioButton`が存在する場合は、それにフォーカスを移動する -- パネルに設定した`FocusTraversalPolicy`を有効にするために、`JPanel#setFocusTraversalPolicyProvider(true);`を設定する必要がある -- `JRadioButton`を配置したパネルに`getDefaultComponent(...)`メソッドをオーバーライドした`FocusTraversalPolicy`を設定し現在選択されている`JRadioButton`が存在する場合は、そこにフォーカスが移動する -- パネルに設定した`FocusTraversalPolicy`を有効にするために`JPanel#setFocusTraversalPolicyProvider(true);`を設定する必要がある * 参考リンク [#reference] - [http://stackoverflow.com/questions/34820018/get-the-focus-in-a-buttongroup-of-jradiobuttons-to-go-to-the-currently-selected/34832814#34832814 java - Get the focus in a ButtonGroup of JRadioButtons to go to the currently selected item instead of first - Stack Overflow] - [https://stackoverflow.com/questions/34820018/get-the-focus-in-a-buttongroup-of-jradiobuttons-to-go-to-the-currently-selected/34832814#34832814 java - Get the focus in a ButtonGroup of JRadioButtons to go to the currently selected item instead of first - Stack Overflow] - [https://bugs.openjdk.org/browse/JDK-8033699 [JDK-8033699] Incorrect radio button behavior - Java Bug System] - [https://bugs.openjdk.org/browse/JDK-8154043 [JDK-8154043] Fields not reachable anymore by tab-key, because of new tabbing behaviour of radio button groups. - Java Bug System] * コメント [#comment] #comment #comment