之前已经处理过这种类型的帖子,但我的问题基于我的代码的结构。
我只是想在我上一栏的所有行中添加一个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));
当我运行此代码时,它只在第3列显示为true,而不是JcomboBox? 任何人都可以发现问题吗? 非常感谢