Swing/ClearComboBoxItemSelection のバックアップの現在との差分(No.6)
- 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
概要
JComboBox
の項目が何も選択されていない状態になるよう設定します。
Screenshot
Advertisement
サンプルコード
combo.setSelectedIndex(-1);
#spandel
//or: combo.setSelectedItem(null);
#spanend
#spanadd
// or: combo.setSelectedItem(null);
#spanend
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JComboBox#setSelectedIndex(-1)
やJComboBox#setSelectedItem(null)
でJComboBox
の項目がどれも選択されていない状態にするテストを行っています。
- JDK-4180057 JComboBox needs to document that setSelectedIndex( -1 ) means no selection - Java Bug System
- 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
以外の負の値は範囲外となり、例外が発生する:- 項目数が
== 0
の場合は-1
- 項目数が
> 0
の場合は0
- 項目数が
-
-1
以外の負の値は範囲外となり例外が発生するjava.lang.IllegalArgumentException: setSelectedIndex: -2 out of bounds
参考リンク
- JComboBox#setSelectedIndex(int) (Java Platform SE 8)
- JDK-4180057 JComboBox needs to document that setSelectedIndex( -1 ) means no selection - Java Bug System
- JDK-4180057 JComboBox needs to document that setSelectedIndex( -1 ) means no selection - Java Bug System