Swing/ButtonGroupEmptySelection のバックアップ差分(No.1)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ButtonGroupEmptySelection へ行く。
- 1 (2018-12-10 (月) 16:21:32)
- 2 (2019-05-07 (火) 19:25:24)
- 3 (2019-05-16 (木) 14:00:23)
- 4 (2019-11-28 (木) 15:42:02)
- 5 (2019-12-10 (火) 17:30:06)
- 6 (2021-06-11 (金) 15:01:28)
- 7 (2025-01-03 (金) 08:57:02)
- 8 (2025-01-03 (金) 09:01:23)
- 9 (2025-01-03 (金) 09:02:38)
- 10 (2025-01-03 (金) 09:03:21)
- 11 (2025-01-03 (金) 09:04:02)
- 12 (2025-06-19 (木) 12:41:37)
- 13 (2025-06-19 (木) 12:43:47)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: ButtonGroupEmptySelection title: ButtonGroup内のボタンが選択されているかを確認する tags: [ButtonGroup, JToggleButton] author: aterai pubdate: 2018-12-10T16:19:43+09:00 description: ButtonGroup内のボタンが1つも選択されていない状態かどうかを確認します。 image: https://drive.google.com/uc?id=1EFXEFMWUlrctxFnVQEQbK1zeW3wSIhoROw --- * 概要 [#summary] `ButtonGroup`内のボタンが1つも選択されていない状態かどうかを確認します。 #download(https://drive.google.com/uc?id=1EFXEFMWUlrctxFnVQEQbK1zeW3wSIhoROw) * サンプルコード [#sourcecode] #code(link){{ button.addActionListener(e -> { String txt = Optional.ofNullable(bg.getSelection()) .map(b -> String.format("\"%s\" isSelected.", b.getActionCommand())) .orElse("Please select one of the option above."); label.setText(txt); // ButtonModel bm = bg.getSelection(); // if (bm != null) { // label.setText(String.format("\"%s\" isSelected.", bm.getActionCommand())); // } else { // label.setText("Please select one of the option above."); // } }); }} * 解説 [#explanation] 上記のサンプルでは、`ButtonGroup`内のボタンが`1`つも選択されていない状態かどうかを`ButtonModel#getSelection()`メソッドが`null`を返すかどうかで確認しています。 - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/ButtonGroup.html#getSelection-- ButtonGroup##getSelection() (Java Platform SE 8)]には、「選択されたボタンのモデルを返します。」と`1`つも選択されていない状態で何が返されるかは記述されていない -- `ButtonModel`のソースコードではその場合、`null`が返る実装になっている * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/ButtonGroup.html#getSelection-- ButtonGroup##getSelection() (Java Platform SE 8)] - [[ButtonGroup中にある選択状態のJToggleButtonをクリックして選択解除可能にする>Swing/ToggleButtonGroup]] * コメント [#comment] #comment #comment