Swing/ButtonGroupFocusTraversalPolicy のバックアップ差分(No.3)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - 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)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- title: ButtonGroup内で最初にフォーカスを取得するJRadioButtonを変更する tags: [ButtonGroup, FocusTraversalPolicy, JRadioButton, Focus] author: aterai pubdate: 2016-01-18T00:09:46+09:00 description: Tabキーによるフォーカス移動が発生した場合、ButtonGroup内で現在選択されているJRadioButtonにフォーカスが移動するよう設定します。 --- * 概要 [#j7b435e2] KBD{Tab}キーによるフォーカス移動が発生した場合、`ButtonGroup`内で現在選択されている`JRadioButton`にフォーカスが移動するよう設定します。 #download(https://lh3.googleusercontent.com/-r6C4rorBCYw/Vpuu38No8lI/AAAAAAAAOLc/6xa7VoLpsWI/s800-Ic42/ButtonGroupFocusTraversalPolicy.png) * サンプルコード [#g44dcd14] #code(link){{ buttons.setFocusTraversalPolicyProvider(true); buttons.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() { @Override public Component getDefaultComponent(Container focusCycleRoot) { ButtonModel selection = bg.getSelection(); for (Component c: focusCycleRoot.getComponents()) { JRadioButton radioBtn = (JRadioButton) c; ButtonModel loopModel = radioBtn.getModel(); if (loopModel == selection) { return radioBtn; JRadioButton r = (JRadioButton) c; if (r.getModel().equals(selection)) { return r; } } return super.getDefaultComponent(focusCycleRoot); } }); }} * 解説 [#t1739603] - `Default` -- デフォルトの`LayoutFocusTraversalPolicy`では、`JRadioButton`の選択状態には無関係に、レイアウトの先頭にある`JRadioButton`にフォーカスが移動する - `FocusTraversalPolicy` -- `JRadioButton`を配置したパネルに`getDefaultComponent(...)`メソッドをオーバーライドした`FocusTraversalPolicy`を設定し、現在選択されている`JRadioButton`が存在する場合は、それにフォーカスを移動する -- パネルに設定した`FocusTraversalPolicy`を有効にするために、`JPanel#setFocusTraversalPolicyProvider(true);`を設定する必要がある * 参考リンク [#z628475c] - [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] * コメント [#b4307a3f] #comment #comment