• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JComboBoxの高さを変更する
#navi(../)
#tags(JComboBox, ListCellRenderer)
RIGHT:Posted by &author(aterai); at 2009-03-02
* JComboBoxの高さを変更する [#fd467807]
---
title: JComboBoxの高さを変更する
tags: [JComboBox, ListCellRenderer]
author: aterai
pubdate: 2009-03-02T12:37:58+09:00
description: JComboBox自体の高さや、ドロップダウンリスト内にあるアイテムの高さを変更します。
---
* 概要 [#fd467807]
`JComboBox`自体の高さや、ドロップダウンリスト内にあるアイテムの高さを変更します。

- &jnlp;
- &jar;
- &zip;
#download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTJ6VVptrI/AAAAAAAAAVI/x72zWGymqHk/s800/ComboItemHeight.png)

#ref(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTJ6VVptrI/AAAAAAAAAVI/x72zWGymqHk/s800/ComboItemHeight.png)

** サンプルコード [#n266cd55]
* サンプルコード [#n266cd55]
#code(link){{
JComboBox combo1 = new JComboBox(items);
JLabel renderer1 = (JLabel)combo1.getRenderer();
renderer1.setPreferredSize(new Dimension(0, 32));

JComboBox combo2 = new JComboBox(items);
final ListCellRenderer r = combo2.getRenderer();
final Dimension dim = ((JLabel)r).getPreferredSize();
combo2.setRenderer(new ListCellRenderer() {
  @Override public Component getListCellRendererComponent(
        JList list, Object value, int index,
        boolean isSelected, boolean cellHasFocus) {
    Component c = r.getListCellRendererComponent(
      list, value, index, isSelected, cellHasFocus);
    c.setPreferredSize(new Dimension(100, (index<0)?dim.height:32));
    return c;
  }
});
}}

** 解説 [#e555ddb2]
* 解説 [#e555ddb2]
- 上
-- レンダラーに`setPreferredSize`で高さを設定しています。

- 下
-- レンダラーの`getListCellRendererComponent`で、`index`が`0`以上の時だけ、高さを変更しています。

//**参考リンク
** コメント [#yac29798]
//* 参考リンク
* コメント [#yac29798]
#comment
- `html`タグを使用するサンプルなどを追加。 -- &user(aterai); &new{2013-12-20 (金) 20:06:03};

#comment