Swing/ComboBoxPlaceholder のバックアップの現在との差分(No.4)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ComboBoxPlaceholder へ行く。
- 1 (2017-05-01 (月) 18:57:27)
- 2 (2017-12-01 (金) 18:09:47)
- 3 (2018-02-15 (木) 14:23:42)
- 4 (2018-03-28 (水) 18:16:29)
- 5 (2018-04-09 (月) 15:11:19)
- 6 (2018-04-12 (木) 13:09:08)
- 7 (2018-04-18 (水) 20:50:10)
- 8 (2018-04-19 (木) 13:46:24)
- 9 (2018-04-20 (金) 17:18:54)
- 10 (2018-11-19 (月) 22:17:14)
- 11 (2018-11-26 (月) 20:06:38)
- 12 (2019-05-24 (金) 19:23:12)
- 13 (2019-08-14 (水) 15:41:21)
- 14 (2019-08-15 (木) 16:04:08)
- 15 (2019-09-25 (水) 21:28:20)
- 16 (2019-10-02 (水) 13:44:20)
- 17 (2020-03-23 (月) 20:13:59)
- 18 (2021-09-29 (水) 11:34:26)
- 19 (2022-08-12 (金) 00:58:07)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: ComboBoxPlaceholder title: JComboBoxでアイテムが選択されていない場合のプレースホルダ文字列を設定する tags: [JComboBox] tags: [JComboBox, ListCellRenderer] author: aterai pubdate: 2017-05-01T14:53:34+09:00 description: JComboBoxでアイテムが選択されていない場合、代わりに表示するプレースホルダ文字列を設定します。 image: https://drive.google.com/uc?id=1R3IHJMxqNMm4oHGv9wmZ8FXpeZJn0AvEwA --- * 概要 [#summary] `JComboBox`でアイテムが選択されていない場合、代わりに表示するプレースホルダ文字列を設定します。 #download(https://drive.google.com/uc?id=1R3IHJMxqNMm4oHGv9wmZ8FXpeZJn0AvEwA) * サンプルコード [#sourcecode] #code(link){{ JComboBox<String> combo1 = new JComboBox<>(new String[] {"One", "Two", "Three", "Four"}); JComboBox<String> combo1 = new JComboBox<>( new String[] {"One", "Two", "Three", "Four"}); combo1.setSelectedIndex(-1); combo1.setRenderer(new DefaultListCellRenderer() { @Override public Component getListCellRendererComponent( JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { //XXX: String str = index < 0 ? "- Select Item -" : value.toString(); JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { // ???: String str = index < 0 ? "- Select Item -" : value.toString(); String str = Objects.toString(value, "- Select Item -"); super.getListCellRendererComponent(list, str, index, isSelected, cellHasFocus); super.getListCellRendererComponent( list, str, index, isSelected, cellHasFocus); return this; } }); }} * 解説 [#explanation] - `DefaultListCellRenderer#getListCellRendererComponent(...)`メソッドをオーバーライドし、引数の値が`null`の場合のみ代わりにプレースホルダ文字列を表示するコンポーネントを返す -- インデックスが`-1`の場合にプレースホルダ文字列を表示するように設定すると、選択が変更できなくなる? -- `JComboBox`のモデルにはプレースホルダ文字列を含める必要がない 上記のサンプルでは、編集不可の`JComboBox`にアイテムが選択されていない場合にヒントとしてプレースホルダ文字列を表示するリストセルレンダラーを設定しています。 //* 参考リンク [#reference] - `DefaultListCellRenderer#getListCellRendererComponent(...)`メソッドをオーバーライドし、引数の`value`が`null`である場合のみヒント文字列を表示するよう設定 -- 引数の`Object value`が`null`の場合ではなくリストの`index`が`-1`の場合にヒント文字列を表示すると、マウスクリックで`JComboBox`の選択状態が変更不可になる? - リストセルレンダラー側でプレースホルダ文字列を表示しているため`JComboBox`のモデル側にプレースホルダ文字列を含める必要がない * 参考リンク [#reference] - [[JTextFieldにフォーカスと文字列が無い場合の表示>Swing/GhostText]] - [[JTextFieldに透かし画像を表示する>Swing/WatermarkInTextField]] - [[JPasswordFieldにヒント文字列を描画する>Swing/InputHintPasswordField]] - [[ComboBoxEditorにJLayerを設定してプレースホルダ文字列を表示する>Swing/ComboEditorPlaceholder]] -- こちらは編集可能な`JComboBox`でプレースホルダ文字列を表示する場合のサンプル * コメント [#comment] #comment #comment