• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTableのCellRendererにJComboBoxを設定
#navi(../)
RIGHT:Posted by [[terai]] at 2007-03-19
*JTableのCellRendererにJComboBoxを設定 [#xc6bebbb]
JTableのCellRendererとしてJComboBoxを使用します。
---
category: swing
folder: ComboCellRenderer
title: JTableのCellRendererにJComboBoxを設定
tags: [JTable, JComboBox, TableCellRenderer, TableCellEditor]
author: aterai
pubdate: 2007-03-19T02:32:35+09:00
description: JTableのCellRendererとしてJComboBoxを使用します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTJ35Po_yI/AAAAAAAAAVE/z4Jn6Mv7-pc/s800/ComboCellRenderer.png
---
* 概要 [#summary]
`JTable`の`CellRenderer`として`JComboBox`を使用します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTJ35Po_yI/AAAAAAAAAVE/z4Jn6Mv7-pc/s800/ComboCellRenderer.png)

#screenshot

**サンプルコード [#l43fa315]
#code{{
* サンプルコード [#sourcecode]
#code(link){{
class ComboCellRenderer extends JComboBox implements TableCellRenderer {
  private static final Color ec = new Color(240, 240, 255);
  private final JTextField editor;
  public ComboCellRenderer() {
    super();
    setEditable(true);
    setBorder(BorderFactory.createEmptyBorder());

    editor = (JTextField) getEditor().getEditorComponent();
    editor.setBorder(BorderFactory.createEmptyBorder());
    editor.setOpaque(true);
  }
  public Component getTableCellRendererComponent(JTable table, Object value,
                           boolean isSelected, boolean hasFocus,
                           int row, int column) {

  @Override public Component getTableCellRendererComponent(
      JTable table, Object value,
      boolean isSelected, boolean hasFocus,
      int row, int column) {
    removeAllItems();
    if(isSelected) {
    if (isSelected) {
      editor.setForeground(table.getSelectionForeground());
      editor.setBackground(table.getSelectionBackground());
    }else{
    } else {
      editor.setForeground(table.getForeground());
      editor.setBackground((row%2==0)?ec:table.getBackground());
      editor.setBackground((row % 2 == 0) ? ec : table.getBackground());
    }
    addItem((value==null)?"":value.toString());
    addItem(Objects.toString(value, ""));
    return this;
  }
}
}}

**解説 [#k4fd12f0]
上記のサンプルでは、1列目(中央)のセルの表示をJComboBoxにするために、これを継承するセルレンダラーを設定しています。1列目はセルエディタもJComboBoxですが、これらは同じJComboBoxのインスタンスではなく、別々に用意しています。
* 解説 [#explanation]
- `1`列目(中央)のセルの表示を`JComboBox`にするために、これを継承するセルレンダラーを設定
-- この列のセルエディタも`JComboBox`を使用するが、セルレンダラーとは別の`JComboBox`のインスタンスを設定
- セルレンダラーとして使用する`JComboBox`はセルの表示のみに使用するため、以下のように設定
-- 表示用のアイテム(文字列)を一つだけ持つ
-- 編集可にして`EditorComponent`の背景色などを他のセルと同様になるように変更
-- セル内にきれいに収まるように余白を`0`に設定

レンダラーで使用するJComboBoxは、セルの表示のみに使用するため、以下のように設定しています。
-表示用のアイテム(文字列)を一つだけ持つ
-編集可にしてEditorComponentの背景色などを他のセルと同様になるように変更
-セル内にきれいに収まるように余白を0に設定
* 参考リンク [#reference]
- [[JTableのCellEditorにJComboBoxを設定>Swing/ComboCellEditor]]

**参考リンク [#ta6ab55a]
-[[JTableのCellEditorにJComboBoxを設定>Swing/ComboCellEditor]]
* コメント [#comment]
#comment
- セルの幅を大きくするとセルの表示が消えますが・・・これは一体なんでしょうか? -- &user(ichikawa); &new{2010-11-01 (月) 23:09:02};
-- ご指摘ありがとうございます。リサイズなどでセルの表示がおかしくなるのは、バグです。`DefaultTableCellRenderer#invalidate()`などと同じ(パフォーマンス上の理由)にするため、何もしないようにオーバーライドした、テスト中のコードとサンプルを誤ってアップロードしていました。 -- &user(aterai); &new{2010-11-02 (火) 14:23:32};

**コメント [#u7323d9c]
- セルの幅を大きくするとセルの表示が消えますが・・・これは一体なんでしょうか? -- [[ichikawa]] &new{2010-11-01 (月) 23:09:02};

#comment