2009-01-19 62 views
14

我想将单个JComboBox放入JTable的每个单元格中。即。每个单元的JComboBox内容都不相同。将JComboBox放入JTable中

我基本上希望能够调用下面的代码将一行JComboBox添加到JTable中。任何人有任何想法?谢谢

JComboBox cb1 = new JComboBox(...); 
JComboBox cb2 = new JComboBox(...); 
model.addRow(new Object[] {"Row name", cb1, cb2}); 

JComboBox cb3 = new JComboBox(...); 
JComboBox cb4 = new JComboBox(...); 
model.addRow(new Object[] {"Row name 2", cb3, cb4}); 

我能找到的最接近的示例代码如下。但是对于JComboBox内容对于单个列而言是相同的。不是我需要的解决方案。

TableColumn col = table.getColumnModel().getColumn(vColIndex); 
col.setCellEditor(new MyComboBoxEditor(values)); 

其中

public class MyComboBoxEditor extends DefaultCellEditor { 
    public MyComboBoxEditor(String[] items) { 
     super(new JComboBox(items)); 
    } 
} 
+0

非常简单:表.getColumnModel()。getColumn(2).setCellEditor(new DefaultCellEditor(myComboBox));在那里你明显地加载你的值myComboBox。你不需要任何额外的课程! – Elmue 2016-12-24 04:15:09

回答

-8

最简单的方法是实现自己的TableModel

public class MyModel extends AbstractTableModel { 
    List rows; 

    public int getRowCount() { 
     return rows.size(); 
    } 

    public int getColumnCount() { 
     return 4; 
    } 

    public Object getValueAt(int row, int column) { 
     return rows.get(row).getCol(col); //assuming your row "Object" has a getCol() 
    } 

    public Class<?> getColumnClass(int col) { 
     return Boolean.class; 
    } 

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 
     rows.get(rowIndex).getCol(columnIndex).setValue(aValue); 
    } 

} 

加载到这一点,你的JTable。如果您还没有替换Boolean的默认单元格渲染器,则由于您实现了getColumnClass(),所有单元格都将显示为复选框。所有用户输入到这些复选框都是用我们的setValueAt()收集的。

+20

Err,他问了一个JComboBox,而不是JCheckBox。这个答案如何被接受? – 2011-03-07 17:44:10

+0

@SarelBotha因为OP最后一次出现在09年1月31日,正确的答案是从2009年6月3日。显然没有人认为这是一个问题。参见[meta](http://meta.stackexchange.com/questions/161946/rethinking-sort-order-of-answers)。 – PiTheNumber 2013-02-04 07:06:14

0

This page可以帮助你,但看来你是仅限于具有一列所有单元相同的组合框。

2

你需要重写:

Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 

...在TableCellEditor的。传递给此方法的值是您可以放入JComboBox的值。这意味着特定单元格的'价值'需要是可以转化为集合的东西。它可能只是一个对象列表,或者它可能是一个POJO,可以将它们制作成JComboBox。

因此,只需编辑MyComboBoxEditor来覆盖该方法并更改模型以允许实际表示其他几个对象的对象。

0

您需要创建JTable的子类来覆盖方法TableCellEditor getCellEditor(int row,int column)。

这使您可以为任何行和列组合设置任意单元格编辑器。默认的方法是为整个列设置单元格编辑器。

(您也可以通过重写getCellRenderer设置单独的单元格渲染器。)

9

扩展JTable中使用此代码:

@Override 
public TableCellEditor getCellEditor(int row, int column) { 
    Object value = super.getValueAt(row, column); 
    if(value != null) { 
     if(value instanceof JComboBox) { 
      return new DefaultCellEditor((JComboBox)value); 
     } 
      return getDefaultEditor(value.getClass()); 
    } 
    return super.getCellEditor(row, column); 
} 

这将创建的每个组合中,您得到了一个独特的JComboBox中单元格编辑器价值。

+3

+1 - 还应该在相应列的`TableColumnModel`上设置一个自定义的`TableCellRenderer`,以确保所选值被绘制而不是字符串`javax.swing.JCombobox [...]`而单元格未被编辑。这个`TableCellRenderer`应该实现`getTableCellRendererComponent(..)`,并且可以返回一个`JLabel`,其值为'JComboBox.getSelectedItem()。toString()`(检查空指针后)。 – 2011-06-04 09:24:19

1
@Override 
public TableCellEditor getCellEditor(int row, int column) { 
    Object value = super.getValueAt(row, column); 
    if(value != null) { 
     if(value instanceof JComboBox) { 
      return new DefaultCellEditor((JComboBox)value); 
     } 
      return getDefaultEditor(value.getClass()); 
    } 
    return super.getCellEditor(row, column); 
} 

然后,从JComboBox重写toString方法。

2

我相信这会解决您的问题。提及您需要在哪个列中设置组合框。getColumn(INT列)

private void addComboToTable(JComboBox combo) { 
    TableColumn gradeColumn = YourTable.getColumnModel().getColumn(0); 
    JComboBox comboBox = combo; 
    comboBox.removeAllItems(); 
    try { 
     comboBox.addItem("Item 1"); 
     comboBox.addItem("Item 2"); 
     comboBox.addItem("Item 3"); 
    } catch (NullPointerException e) { 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    gradeColumn.setCellEditor(new DefaultCellEditor(comboBox)); 
} 
2

除了CellEditor的,有必要做cellRenderer的绘制单元格中的组合框,看看这个:

public void example(){ 

     TableColumn tmpColum =table.getColumnModel().getColumn(1); 
     String[] DATA = { "Data 1", "Data 2", "Data 3", "Data 4" }; 
     JComboBox comboBox = new JComboBox(DATA); 

     DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox); 
     tmpColum.setCellEditor(defaultCellEditor); 
     tmpColum.setCellRenderer(new CheckBoxCellRenderer(comboBox)); 
     table.repaint(); 
    } 


/** 
    Custom class for adding elements in the JComboBox. 
*/ 
class CheckBoxCellRenderer implements TableCellRenderer { 
     JComboBox combo; 
     public CheckBoxCellRenderer(JComboBox comboBox) { 
      this.combo = new JComboBox(); 
      for (int i=0; i<comboBox.getItemCount(); i++){ 
       combo.addItem(comboBox.getItemAt(i)); 
      } 
     } 
     public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 
      combo.setSelectedItem(value); 
      return combo; 
     } 
    }