将JComboBox放入JTable

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

我基本上希望能够只调用以下代码将一行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)); } } 

最简单的方法是实现自己的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()收集这些复选框的所有用户输入。

使用以下代码扩展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); } 

这将为您获得值的每个combobox创建一个唯一的JComboBox单元格编辑器。

你需要覆盖:

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

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

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

我相信这会解决你的问题。 提及你需要在哪个列中设置.getColumn(int column)中的combobox

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

对于每个行选择,JComboBox内容呈现相同,因为JTable不提供每列具有多个编辑器的function。 您必须扩展JTable类以支持对行的其他选择。

这篇文章解释得非常好: http : //www.javaworld.com/javaworld/javatips/jw-javatip102.html

除了cellEditor之外,还需要使用cellRenderer在单元格中绘制combobox,请看:

  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 
 @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方法。

此页面可能对您有所帮助,尽管您似乎只能在列中的所有单元格中使用相同的combobox。

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

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

(您还可以通过覆盖getCellRenderer来设置单个单元格渲染器。)