下面是我写来帮忙做以下代码:在JTable中的数据检查重复行
的选择是由一个JComboBox,做出被添加到一个JTable,然后检查,看看哪一个它满足以下三个条件:如果表是空的,则添加它。如果表中存在从组合框中选择的项目,则现有行的qty值将增加1。或者,如果它尚未放在桌子上,它就会被添加。
但是我歌厅如下:很奇怪的现象:
第一行被添加到表中,以“200”(值应为“100”的表是在此之前的空选择)。
从组合框中选择第二个项目,第一行(上面)将其值更改为“100”(这应在第一次选择时发生)。另外,从组合框中选择的第二个项目被添加到表格两次(两个相同的行),值为“300”,值是正确的,但应该只是一行。
从组合第三选择确实如上述2,所以值是正确的,但只应该一行
从组合框中选择一个第四选择被选择,这时要在表中匹配exisiting行,而不是现有的行中更新值也增加了两排的“300”的值...
我想,也许我有环错了,但我现在远远落后于上一项目试图解决这个问题,所以任何帮助都应该大受欢迎......
在此先感谢
final DefaultTableModel model = (DefaultTableModel)main.tillPanel.tblTillSale.getModel();
//populate the combo box
for (int d = 0; d < roundStockObj.length ; d++) {
main.tillPanel.cmbTillProdSelect.addItem(roundStockObj[d].getDescription());
}
//add selection listener to combo
main.tillPanel.cmbTillProdSelect.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
String[] addSelectedItem = new String[4];
selectedItem = main.tillPanel.cmbTillProdSelect.getSelectedItem();
for (int d = 0; d < roundStockObj.length; d++) {
//when selction form combo is matched, an array is created to hold the row data
if (roundStockObj[d].getDescription().equals(selectedItem)) {
addSelectedItem[0] = roundStockObj[d].getDescription();
addSelectedItem[2] = Double.toString(roundStockObj[d].getPrice()).trim();
addSelectedItem[3] = Double.toString(roundStockObj[d].getPrice()).trim();
}
}
main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount());
//if table is empty
for (int rowCount = 0 ; rowCount <= model.getRowCount(); rowCount++) {
if (model.getRowCount() == 0) {
addSelectedItem[1] = "100";
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
//main.tillPanel.tblTillSale.getModel().setValueAt(selectedItem, tillSelectedRow, tillSelectedRow);
main.tillPanel.lblTotPrice.setText("100");
break;
}
// look for duplicate row and if found increase total column of existing row, and not add this selection
if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getValueAt(rowCount, 0))) {
main.tillPanel.lblTotPrice.setText("200");
int currentValue = Integer.parseInt(addSelectedItem[1].trim());
addSelectedItem[1] = "200";
model.setValueAt(addSelectedItem[1], rowCount, 1);
break;
}
//if no duplicate found add this row to the table
else {
addSelectedItem[1] = "300";
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
main.tillPanel.lblTotPrice.setText("300");
break;
}
}
//clear the current selection array of row data
for (int index = 0; index < 4; index++) {
ddSelectedItem[index] = null;
}
}
});
托马斯,大,为解决许多感谢,作品... – user5980196 2011-03-09 19:59:31