TITLE:JTableのCellEditorにJComboBoxを設定
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2005-09-26
*JTableのCellEditorにJComboBoxを設定 [#f57b6c70]
JTableのCellEditorにJComboBoxを使用し、リストから値を選択できるようにします。

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

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTJy9xBM6I/AAAAAAAAAU8/h5YELRcY4gE/s800/ComboCellEditor.png)

**サンプルコード [#y53e1620]
#code(link){{
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());
}}

**解説 [#k4fd12f0]
上記のサンプルでは、1列目のセルエディタとしてコンボボックスを使うDefaultCellEditorを登録しています。

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

-以下は余白を0にしていない場合

//#screenshot(,screenshot1.png)
#ref(http://lh3.ggpht.com/_9Z4BYR88imo/TQTJ1Ykl--I/AAAAAAAAAVA/ZRLgScHCF3s/s800/ComboCellEditor1.png)

----
セルの表示にもJComboBoxを使用する場合は、[[JTableのCellRendererにJComboBoxを設定>Swing/ComboCellRenderer]]を参考にJComboBoxを継承するセルレンダラーを使用してみてください。

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

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