Adding JComboBox to a JTable cell [duplicate]
Asked Answered
E

2

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;

 }
Evapotranspiration answered 16/1, 2013 at 10:2 Comment(2)
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.Willful
existing questionBecky
V
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

Vladivostok answered 16/1, 2013 at 10:19 Comment(4)
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.Jeaz
@Jeaz 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.Xenia
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.Aeneus
with this example the cell looks like a combo box when you click it and are actively editing however otherwise it appears as a regular cell. I feel it is desired to have a way that the editor and renderer both appear as a combo boxKwakiutl
T
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.

Talithatalk answered 16/1, 2013 at 10:11 Comment(5)
Shouldn't you clear the model first before adding stuff to it? Also, you have one too many } after model.addElement() I think...Waterspout
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.Jeaz
@Jeaz coming back to it, it actually answers the "specific cell" part of the question, when the combobox content is different for each row.Waterspout
@Talithatalk Where does orderList.get(i) come from? Eclipse tells me that I need to define orderList and i.Xenia
@Xenia We need to create model for this table via a list. See my other anser here https://mcmap.net/q/905039/-create-jtable-from-arraylist. This might help you.Talithatalk

© 2022 - 2024 — McMap. All rights reserved.