Swing/ClearComboBoxItemSelection のバックアップ(No.15)
- バックアップ一覧
 - 差分 を表示
 - 現在との差分 を表示
 - 現在との差分 - Visual を表示
 - ソース を表示
 - Swing/ClearComboBoxItemSelection へ行く。
  
- 1 (2017-03-27 (月) 13:55:22)
 - 2 (2017-10-05 (木) 14:38:22)
 - 3 (2017-12-26 (火) 17:06:03)
 - 4 (2018-02-15 (木) 14:23:42)
 - 5 (2019-12-20 (金) 14:54:36)
 - 6 (2021-05-31 (月) 15:31:38)
 - 7 (2022-08-20 (土) 22:15:25)
 - 8 (2024-05-20 (月) 12:09:17)
 - 9 (2025-01-03 (金) 08:57:02)
 - 10 (2025-01-03 (金) 09:01:23)
 - 11 (2025-01-03 (金) 09:02:38)
 - 12 (2025-01-03 (金) 09:03:21)
 - 13 (2025-01-03 (金) 09:04:02)
 - 14 (2025-06-19 (木) 12:41:37)
 - 15 (2025-06-19 (木) 12:43:47)
 
 
- category: swing folder: ClearComboBoxItemSelection title: JComboBoxを選択なしの状態にする tags: [JComboBox] author: aterai pubdate: 2017-03-27T13:52:28+09:00 description: JComboBoxの項目が何も選択されていない状態になるよう設定します。 image: https://drive.google.com/uc?id=1ytLrgrvvfQGy-YNgOPiY67cT8EaLV1yHoA
 
Summary
JComboBoxの項目が何も選択されていない状態になるよう設定します。
Screenshot

Advertisement
Source Code Examples
Description
上記のサンプルでは、JComboBox#setSelectedIndex(-1)やJComboBox#setSelectedItem(null)でJComboBoxの項目がどれも選択されていない状態にするテストを行っています。
- [JDK-4180057] JComboBox needs to document that setSelectedIndex( -1 ) means no selection - Java Bug System
JComboBox#setSelectedIndex(-1)で選択状態がクリアされるのは当初からの仕様だが、ドキュメントに記述されたのは1.4.0からになる(日本語版は1.7.0から)
 JComboBoxのアイテム選択の初期状態:- 項目数が
== 0の場合は-1 - 項目数が
> 0の場合は0 
- 項目数が
 -1以外の負の値は範囲外となり例外が発生するjava.lang.IllegalArgumentException: setSelectedIndex: -2 out of bounds
Reference
- JComboBox#setSelectedIndex(int) (Java Platform SE 8)
 - [JDK-4180057] JComboBox needs to document that setSelectedIndex( -1 ) means no selection - Java Bug System