• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTableのCellEditorにJComboBoxを設定
#navi(../)
*JTableのCellEditorにJComboBoxを設定 [#f57b6c70]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2005-09-26~
更新日:&lastmod;
---
category: swing
folder: ComboCellEditor
title: JTableのCellEditorにJComboBoxを設定
tags: [JTable, TableCellEditor, JComboBox, TableColumn]
author: aterai
pubdate: 2005-09-26T15:27:12+09:00
description: JTableのCellEditorにJComboBoxを使用し、リストから値を選択できるようにします。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTJy9xBM6I/AAAAAAAAAU8/h5YELRcY4gE/s800/ComboCellEditor.png
---
* 概要 [#summary]
`JTable`の`CellEditor`に`JComboBox`を使用し、リストから値を選択できるようにします。

#contents
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTJy9xBM6I/AAAAAAAAAU8/h5YELRcY4gE/s800/ComboCellEditor.png)

**概要 [#v4c613bd]
JTableのCellEditorにJComboBoxを使用し、リストから値を選択できるようにします。
* サンプルコード [#sourcecode]
#code(link){{
JComboBox cb = new JComboBox(new String[] {"名前0", "名前1", "名前2"});
cb.setBorder(BorderFactory.createEmptyBorder());

#screenshot
TableColumn col = table.getColumnModel().getColumn(1);
col.setCellEditor(new DefaultCellEditor(cb));
// col.setCellRenderer(new ComboBoxCellRenderer());
}}

**サンプルコード [#y53e1620]
 JComboBox cb = new JComboBox(new String[] {"名前0", "名前1", "名前2"});
 cb.setBorder(BorderFactory.createEmptyBorder()); 
 
 TableColumn col = table.getColumnModel().getColumn(1);
 col.setCellEditor(new DefaultCellEditor(cb));
 //col.setCellRenderer(new ComboBoxCellRenderer());
* 解説 [#explanation]
上記のサンプルでは、`1`列目のセルエディタとしてコンボボックスを使う`DefaultCellEditor`を登録しています。

-&jnlp;
-&jar;
-&zip;
- コンボボックスの余白を`0`に設定しないとセル内文字列の上下が欠けてしまう場合がある
#ref(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTJ1Ykl--I/AAAAAAAAAVA/ZRLgScHCF3s/s800/ComboCellEditor1.png)
-- 参考: [http://www.jroller.com/page/santhosh?entry=tweaking_jtable_editing Santhosh Kumar's Weblog : Santhosh Kumar's Weblog]

**解説 [#k4fd12f0]
上記のサンプルでは、1列目のセルエディタとしてコンボボックスを使うDefaultCellEditorを登録しています。
* 参考リンク [#reference]
- [http://www.jroller.com/page/santhosh?entry=tweaking_jtable_editing Santhosh Kumar's Weblog : Santhosh Kumar's Weblog]
- [[JTableのCellRendererにJComboBoxを設定>Swing/ComboCellRenderer]]
-- セルの表示にも`JComboBox`を使用する場合は`JComboBox`を継承するセルレンダラーを使用する
- [[JComboBoxセルエディタのドロップダウンリストを編集開始直後は表示しないよう設定する>Swing/CellEditorTogglePopup]]

コンボボックスの余白を0にしておくと、セル内にきれいにぴったり収まります(参考:[[Santhosh Kumar's Weblog : Santhosh Kumar's Weblog>http://www.jroller.com/page/santhosh?entry=tweaking_jtable_editing]])。
-以下は余白を0にしていない場合
>
#screenshot(,screenshot2.png)

セルの表示にもJComboBoxを使用する場合は、例えば以下のようなJComboBoxを継承するセルレンダラーを使用します。

 class ComboBoxCellRenderer extends JComboBox implements TableCellRenderer {
   private static final Color ec = new Color(240, 240, 255);
   private final JTextField editor;
   public ComboBoxCellRenderer() {
     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) {
     removeAllItems();
     if(isSelected) {
       editor.setForeground(table.getSelectionForeground());
       editor.setBackground(table.getSelectionBackground());
     }else{
       editor.setForeground(table.getForeground());
       editor.setBackground((row%2==0)?ec:table.getBackground());
     }
     addItem((value==null)?"":value.toString());
     return this;
   }
 }

**参考リンク [#w85587e3]
-[[JTable Examples>http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples7.html]]
-[[Santhosh Kumar's Weblog : Santhosh Kumar's Weblog>http://www.jroller.com/page/santhosh?entry=tweaking_jtable_editing]]

**コメント [#naf1e6fc]
* コメント [#comment]
#comment
#comment