JComboBoxのセルサイズを決定するためのプロトタイプ値を設定する
Total: 4125, Today: 1, Yesterday: 1
Posted by aterai at
Last-modified:
Summary
JComboBoxがそのセルサイズを決定するために使用するプロトタイプ値を設定します。
Screenshot

Advertisement
Source Code Examples
combo3.setPrototypeDisplayValue(TITLE);
// ...
combo5.setRenderer(new SiteListCellRenderer<Site>());
combo5.setPrototypeDisplayValue(new Site(TITLE, new ColorIcon(Color.GRAY)));
View in GitHub: Java, KotlinDescription
- デフォルト
- モデルの中からサイズが最大となる要素を検索して
JComboBoxの推奨サイズを決定する - 上記のサンプルではモデルが空なのでボタンの幅と余白が
JComboBoxの推奨サイズになっている
- モデルの中からサイズが最大となる要素を検索して
JComboBox#setPrototypeDisplayValue(...)で指定した要素からJComboBoxの推奨サイズを決定する- 編集可能な場合の
JComboBoxにJComboBox#setPrototypeDisplayValue(...)を設定 - 独自の要素
Eを使用するJComboBox<E>にその要素を表示するためのListCellRenderer<E>を設定 - 上記の
JComboBox<E>にJComboBox#setPrototypeDisplayValue(E)でプロトタイプ値を設定 - 上記の
JComboBox<E>でモデルを空にしてListCellRendererで値がnullになる場合、JComboBox#setPrototypeDisplayValue(E)で設定した値が表示されてしまう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); } // ...
Reference
- JComboBox#setPrototypeDisplayValue(E) (Java Platform SE 8)
- JList#setPrototypeCellValue(E) (Java Platform SE 8)
- JListがJScrollPane内に組み込まれている場合のビューポートサイズを設定する
- JComboBoxなどの幅をカラム数で指定