【问题标题】:Adding JComboBox to a JTable cell [duplicate]将 JComboBox 添加到 JTable 单元格 [重复]
【发布时间】:2012-12-30 14:42:19
【问题描述】:

可能重复:
How to add a JComboBox to a JTable cell?

我发现很难将JComboBox 添加到JTable 的单元格之一,我尝试了下面的代码,但它不起作用..

如何将 jcombobox 添加到特定单元格?

按下enter 时,一个新的jcombobox 应该会自动添加到所需的列中。

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;

 }

【问题讨论】:

  • 您看过已经提出的问题了吗?查看问题右侧的链接,其中许多与您的要求完全匹配。
  • 现有question

标签: java swing jtable jcombobox tablecelleditor


【解决方案1】:

【讨论】:

  • 您可以简单地发布代码,而不是发布指向如此大页面的链接:table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(myComboBox));您可以在其中使用下拉值加载 myComboBox。
  • @Elmue 我同意你的看法!我被“你为什么不读这本庞大的链接教科书,你这个懒惰的人?”之前也发表评论。这不仅没有帮助,而且令人沮丧。
  • 小后续问题,如何使行的高度适应组合框的高度。现在第一行在选择时并不完美。
【解决方案2】:

JComboBox 通过扩展 DefaultCellEditor 添加到 JTable

示例:

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);
  } 
}

通过使用自定义类在 JTable 中创建组件,您可以拥有 更好地控制您正在展示的内容。

【讨论】:

  • 不应该先清除模型再添加东西吗?另外,我认为model.addElement() 之后的} 太多了...
  • 为什么这么复杂??不需要额外的类:table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(myComboBox));您可以在其中使用下拉值加载 myComboBox。
  • @Elmue 回到它,当每行的组合框内容不同时,它实际上回答了问题的“特定单元格”部分。
  • @Amarnath orderList.get(i) 来自哪里? Eclipse 告诉我需要定义 orderList 和 i。
  • @user2303321 我们需要通过列表为这个表创建模型。在这里查看我的其他分析器stackoverflow.com/a/15962751/967638。这可能会对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 2015-03-07
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
  • 2014-03-08
相关资源
最近更新 更多