将JComboBox添加到JTable单元 [英] Adding JComboBox to a JTable cell
本文介绍了将JComboBox添加到JTable单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
Possible Duplicate:
How to add a JComboBox to a JTable cell?I'm finding it difficult to add
JComboBox
to one of the cells of aJTable
, I tried the code below but it's not working..How can I add jcombobox to a particular cell?
On pressing
enter
a newjcombobox
should be added automatically to the desired column.jTable1 = new javax.swing.JTable(); mod=new DefaultTableModel(); mod.addColumn("No"); mod.addColumn("Item ID"); mod.addColumn("Units"); mod.addColumn("Amount"); mod.addColumn("UOM"); mod.addColumn("Delivery Date"); mod.addColumn("Total Amount"); mod.addColumn("Notes"); mod.addColumn("Received"); mod.addRow(new Object [][] { {1, null, null, null, null, null, null, null, null} }); jTable1.setModel(mod); jTable1.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(generateBox())); jTable1.setColumnSelectionAllowed(true); Code to generate ComboBox private JComboBox generateBox() { JComboBox bx=null; Connection con=CPool.getConnection(); try { Statement st=con.createStatement(); String query="select distinct inid from inventory where company_code="+"'"+ims.MainWindow.cc+"'"; ResultSet rs=st.executeQuery(query); bx=new JComboBox(); while(rs.next()){ bx.addItem(rs.getString(1)); } CPool.closeConnection(con); CPool.closeStatement(st); CPool.closeResultSet(rs); }catch(Exception x) { System.out.println(x.getMessage()); } return bx; }
解决方案Look at this link it will help you
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
and the code :
这篇关于将JComboBox添加到JTable单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文