我有JTable
,其中有几列。其中我有JComboBox
。在程序开始时,我希望它们为空。我在按钮的单击操作上有一个 JButton
我有在表中动态添加行的代码。
但是添加行后,我在具有 JComboBox
的单元格中得到垃圾值。如下图所示:
这是代码:
在表中添加 JComboBox 的代码
// Create columns names
String columnNames[] = { "Item", "Sun Item", "Required Quantity","Price","Gross Amount" };
// Create some data
final String dataValues[][] =
{
{ "", "", "","","", },
};
tableModel = new DefaultTableModel(dataValues, columnNames);
// Create a new table instance
table = new JTable( tableModel );
updateItemCombo();
TableColumn itemColumn = table.getColumnModel().getColumn(0);
itemColumn.setCellEditor(new DefaultCellEditor(comboItem));
public void updateItemCombo(){
Vector<String> s = new Vector<String>();
try{
setConnectin();
String str = "select * from ItemTable";
stmt = conn.createStatement();
rs = stmt.executeQuery(str);
while(rs.next())
{
String nm = rs.getString("Item_Name");
s.add(nm);
}
conn.close();
}catch(Exception e2){
e2.printStackTrace();
}
DefaultComboBoxModel<String> modelData = new DefaultComboBoxModel<String>(s);
comboItem.setModel(modelData);
}
单击按钮时动态添加行的代码:
btnAddOrder.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
tableModel.addRow(dataValues);
tableModel.fireTableDataChanged();
}
});
我应该怎么做才能从表中删除这个垃圾值?请帮忙
最佳答案
addRow(...) 方法采用一维数组作为参数。您正在尝试添加二维数组。
此外,请勿使用:
tableModel.fireTableDataChanged();
TableModel 的工作是调用适当的 fireXXX() 方法,在本例中该方法是 fireTableRowsInserted(...)
。
关于java - 动态添加行后jtable中的垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24000615/