这种类型的帖子之前已经处理过,但我遇到了基于我的代码结构的问题。
我只是想在最后一列的所有行中添加一个 JComboBox。代码如下。
//Return Person objects from a method
ArrayList<Person> people = getPersonList();
String[] columnNames {"Name", "Age", "English Speaker?" };
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(columnNames);
JTable table = new JTable(model);
//Create JComboBox for last column (English Speaker?)
JComboBox<Integer> englishCombo = new JComboBox<>();
int count = 1;
//For loop to add each Person to there rows
//Also add a boolean value to determine check box
for(Person p: people)
{
boolean english =false;
if(p.isEnglishSpeaker() == true)
{
english = true;
}
else
{
english = false;
}
questionCombo.addItem(count);
model.addRow(new Object[]{p.getName(), p.getAge(), english);
}
//Get 3rd column (English Speaker)
TableColumn englishColumn = table.getColumnModel().getColumn(2);
//Add JComboBox to English Speaker
englishColumn.setCellEditor(new DefaultCellEditor(englishCombo));
当我运行这段代码时,它只在第三列显示真假,而不是 JcomboBox?谁能找出问题所在?非常感谢