Swing/PrototypeDisplayValue のバックアップ差分(No.2)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/PrototypeDisplayValue へ行く。
- 1 (2016-03-30 (水) 13:45:11)
- 2 (2017-07-19 (水) 19:20:10)
- 3 (2017-08-25 (金) 19:25:03)
- 4 (2017-11-02 (木) 15:34:40)
- 5 (2018-03-12 (月) 18:39:43)
- 6 (2019-05-22 (水) 19:35:38)
- 7 (2020-03-10 (火) 21:03:33)
- 8 (2021-08-27 (金) 11:46:57)
- 9 (2022-08-20 (土) 22:15:25)
- 10 (2024-02-14 (水) 23:41:12)
- 11 (2025-01-03 (金) 08:57:02)
- 12 (2025-01-03 (金) 09:01:23)
- 13 (2025-01-03 (金) 09:02:38)
- 14 (2025-01-03 (金) 09:03:21)
- 15 (2025-01-03 (金) 09:04:02)
- 16 (2025-06-19 (木) 12:41:37)
- 17 (2025-06-19 (木) 12:43:47)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: PrototypeDisplayValue title: JComboBoxのセルサイズを決定するためのプロトタイプ値を設定する tags: [JComboBox, ListCellRenderer] author: aterai pubdate: 2015-05-18T00:01:40+09:00 description: JComboBoxがそのセルサイズを決定するために使用するプロトタイプ値を設定します。 image: https://lh3.googleusercontent.com/-DafUFitik9w/VVidadY7TNI/AAAAAAAAN4U/D40YKz8mMUY/s800/PrototypeDisplayValue.png --- * 概要 [#a3e0c43a] JComboBoxがそのセルサイズを決定するために使用するプロトタイプ値を設定します。 * 概要 [#summary] `JComboBox`がそのセルサイズを決定するために使用するプロトタイプ値を設定します。 #download(https://lh3.googleusercontent.com/-DafUFitik9w/VVidadY7TNI/AAAAAAAAN4U/D40YKz8mMUY/s800/PrototypeDisplayValue.png) * サンプルコード [#p97d1446] * サンプルコード [#sourcecode] #code(link){{ combo3.setPrototypeDisplayValue(TITLE); //... combo5.setRenderer(new SiteListCellRenderer<Site>()); combo5.setPrototypeDisplayValue(new Site(TITLE, new DummyIcon(Color.GRAY))); }} * 解説 [#d353cad6] * 解説 [#explanation] + デフォルトの`JComboBox`で、モデルの中からサイズが最大となる要素を検索し、`JComboBox`の推奨サイズを決定する + `JComboBox#setPrototypeDisplayValue(...)`で指定した要素から、`JComboBox`の推奨サイズを決定する + 編集可能な場合の`JComboBox`に`JComboBox#setPrototypeDisplayValue(...)`を設定 ++ [http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6422966 Bug ID: JDK-6422966 Editable JComboBox.setPrototypeDisplayValue: size can not be smaller than editor] + 独自の要素`E`を使用する`JComboBox<E>`に、その要素を表示するための`ListCellRenderer<E>`を設定 + 上記の`JComboBox<E>`に`JComboBox#setPrototypeDisplayValue(E)`でプロトタイプ値を設定 + 上記の`JComboBox<E>`でモデルを空にして`ListCellRenderer`で値が`null`になる場合、`JComboBox#setPrototypeDisplayValue(E)`で設定した値が表示されてしまう #code{{ class SiteListCellRenderer<E extends Site> extends JLabel implements ListCellRenderer<E> { @Override public Component getListCellRendererComponent( JList<? extends E> list, E value, int index, boolean isSelected, boolean cellHasFocus) { setOpaque(index >= 0); if (Objects.nonNull(value)) { setText(value.title); setIcon(value.favicon); } else { //JComboBox#setPrototypeDisplayValue(E)で設定した値が表示されないようにクリア setText(""); setIcon(null); } //... }} * 参考リンク [#q7636696] * 参考リンク [#reference] - [[JComboBoxなどの幅をカラム数で指定>Swing/SetColumns]] * コメント [#p7e72111] * コメント [#comment] #comment #comment