我想在JTable Cell中添加一个JComboBox,

时间:2015-01-22 12:24:28

标签: java swing jtable jcombobox

我正在尝试在JComboBox单元格中添加JTable。 在行向量

中添加JComboBox之后
Vector rown=new Vector();
rown.add(comboBox1);

并运行我的GUI,表格显示:

javax.swing.JComboBox[,0,0,0x0,invalid,layout=javax.swing.plaf.metal.MetalComboBoxUI$MetalComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=16777536,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=VALUE_ONE

而不是VALUE_ONE中的JComboBox

我哪里错了?

1 个答案:

答案 0 :(得分:0)

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

public class CustomComboBoxEditor extends DefaultCellEditor {
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) {
    model.addElement(orderList.get(i));
 } 


   return super.getTableCellEditorComponent(table, value, isSelected, row, column);
 } 
}