TITLE:JComboBoxの高さを変更する
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2009-03-02
*JComboBoxの高さを変更する [#fd467807]
JComboBox自体の高さや、ドロップダウンリスト内にあるアイテムの高さを変更します。

-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(http://lh3.ggpht.com/_9Z4BYR88imo/TQTJ6VVptrI/AAAAAAAAAVI/x72zWGymqHk/s800/ComboItemHeight.png)

**サンプルコード [#n266cd55]
#code(link){{
JComboBox combo1 = new JComboBox(items);
JLabel renderer1 = (JLabel)combo1.getRenderer();
renderer1.setPreferredSize(new Dimension(0, 32));
}}
#code{{
JComboBox combo2 = new JComboBox(items);
final ListCellRenderer r = combo2.getRenderer();
final Dimension dim = ((JLabel)r).getPreferredSize();
combo2.setRenderer(new ListCellRenderer() {
  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]
-上
--レンダラーにsetPreferredSizeで高さを設定しています。

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

//**参考リンク
**コメント [#yac29798]
#comment