13

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 a JTable, I tried the code below but it's not working..

How can I add jcombobox to a particular cell?

On pressing enter a new jcombobox 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;

 }
Community
  • 1
  • 1
c.pramod
  • 606
  • 1
  • 8
  • 22
  • Did you take a look at the already asked questions? Look at the links on the right of your question, many of them match exactly what you are asking for. – Guillaume Polet Jan 16 '13 at 10:05
  • existing [question](http://stackoverflow.com/questions/2059229/how-to-add-a-jcombobox-to-a-jtable-cell?rq=1) – TheWhiteRabbit Jan 16 '13 at 10:10

2 Answers2

14

Look at this link it will help you

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html enter image description here

and the code :

http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableRenderDemoProject/src/components/TableRenderDemo.java

Alya'a Gamal
  • 5,624
  • 19
  • 34
  • 11
    Instead of posting a link to such a large page you could simply post the code: table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(myComboBox)); where you obvioulsy load myComboBox with your drop down values. – Elmue Dec 24 '16 at 04:12
  • @Elmue I agree with you! I've been hit by the, "Why don't you just read this massive textbook of a link, you lazy person?" comment before as well. Not only is it unhelpful, it's discouraging. – user2303321 Oct 29 '18 at 23:31
  • small follow up question, how do I make the height of the row adapt to that of the combo box. Now the first line doesn't fit perfectly while selecting. – Gilles Lesire May 14 '19 at 11:53
9

JComboBox is added to JTable by extending DefaultCellEditor

Example:

TableColumn comboCol1 = table.getColumnModel().getColumn(0);
comboCol1.setCellEditor(new CustomComboBoxEditor());

/**
   Custom class for adding elements in the JComboBox.
*/
public class CustomComboBoxEditor extends DefaultCellEditor {

  // Declare a model that is used for adding the elements to the `Combo box`
  private DefaultComboBoxModel model;

  public CustomComboBoxEditor() {
      super(new JComboBox());
      this.model = (DefaultComboBoxModel)((JComboBox)getComponent()).getModel();
  }

  @Override
  public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
      // Add the elements which you want to the model.
      // Here I am adding elements from the orderList(say) which you can pass via constructor to this class.
      model.addElement(orderList.get(i));

      //finally return the component.
      return super.getTableCellEditorComponent(table, value, isSelected, row, column);
  } 
}

By using a custom class to create Components in JTable you can have more control over the content which your are showing.

Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • 1
    Shouldn't you clear the model first before adding stuff to it? Also, you have one too many `}` after `model.addElement()` I think... – Matthieu Apr 19 '16 at 12:43
  • Why so complicated?? No additional class is needed: table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(myComboBox)); where you obvioulsy load myComboBox with your drop down values. – Elmue Dec 24 '16 at 04:11
  • 1
    @Elmue coming back to it, it actually answers the "specific cell" part of the question, when the combobox content is different for each row. – Matthieu Feb 14 '17 at 15:34
  • @Amarnath Where does orderList.get(i) come from? Eclipse tells me that I need to define orderList and i. – user2303321 Oct 30 '18 at 00:01
  • @user2303321 We need to create model for this table via a list. See my other anser here https://stackoverflow.com/a/15962751/967638. This might help you. – Amarnath Oct 31 '18 at 16:02