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

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

//#screenshot
#ref(http://lh3.ggpht.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));
}}
#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で高さを設定しています。
** 解説 [#e555ddb2]
- 上
-- レンダラーに`setPreferredSize`で高さを設定しています。

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

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