这种类型的帖子之前已经处理过,但我遇到的问题是基于我的代码的结构。将JCombo列添加到JTable
我只是简单地试图添加一个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中? 任何人都可以找出问题吗? 非常感谢
'布尔英语= FALSE; if(p.isEnglishSpeaker()== true) { english = true; } else { english = false; '也可以写成:'boolean english = p.isEnglishSpeaker();'。或者甚至更简单,放下之前写的所有东西,只需调用:'model.addRow(new Object [] {p.getName(),p.getAge(),p.isEnglishSpeaker());' – 2013-05-05 10:27:41
查看本[回答]( http://stackoverflow.com/questions/11226926/java-jtable-with-jcombobox/11227034#11227034)。 – 2013-05-05 10:35:40
感谢您的回复。我只是改变了更简单的方式(不知道我是如何错过的)。但它仍然只返回false,而不是实际的JComboBox?谢谢 – 2013-05-05 10:36:07