2011-03-08 165 views
0

下面是我写来帮忙做以下代码:在JTable中的数据检查重复行

的选择是由一个JComboBox,做出被添加到一个JTable,然后检查,看看哪一个它满足以下三个条件:如果表是空的,则添加它。如果表中存在从组合框中选择的项目,则现有行的qty值将增加1。或者,如果它尚未放在桌子上,它就会被添加。

但是我歌厅如下:很奇怪的现象:

  1. 第一行被添加到表中,以“200”(值应为“100”的表是在此之前的空选择)。

  2. 从组合框中选择第二个项目,第一行(上面)将其值更改为“100”(这应在第一次选择时发生)。另外,从组合框中选择的第二个项目被添加到表格两次(两个相同的行),值为“300”,值是正确的,但应该只是一行。

  3. 从组合第三选择确实如上述2,所以值是正确的,但只应该一行

  4. 从组合框中选择一个第四选择被选择,这时要在表中匹配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; 
     } 
    } 
}); 

回答

0

编辑:等待,为什么你如果要不内部的各个break语句。你的循环只会运行一次。所以如果它没有找到第一次通过的值,它将退出。

取出break语句。还要移动if语句来检查表是否在循环外是空的。

0

for (int rowCount = 0 ; rowCount <= model.getRowCount(); rowCount++)

我猜你的意思是

for (int rowCount = 0 ; rowCount < model.getRowCount(); rowCount++)

否则你会得到的最后一次迭代的IndexOutOfBoundException(当rowCount == model.getRowCount())。

编辑:

//查找重复的行,如果发现现有行的增长总列,而不是添加此选择

你不找一个重复的行,但检查第一个行是否重复。如果不是,则再次添加相同的项目(使用“300”)。

这会更好:

//model is empty 
if(model.getRowCount() == 0) { 
    //add first (case "100") 
} 
//model is not empty 
else { 
    //check if there is a duplicate row 
    int duplicateRow = -1; 
    for (int row = 0 ; row < model.getRowCount(); row++) { 
     if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getValueAt(row,0))) { 
     duplicateRow = row; 
     break; 
     } 
    } 
    //if there is no duplicate row, append 
    if(duplicateRow == -1) { 
    //append (case "300") 
    } 
    //if there is a duplicate row, update 
    else { 
    //update row with index duplicateRow (case "200") 
    }  
} 
+0

托马斯,大,为解决许多感谢,作品... – user5980196 2011-03-09 19:59:31

0

望着代码我强烈怀疑,问题是休息,他们将你的for循环到如果其他设置。他们是你只看第一行的原因,我怀疑你的意图是继续使用而不是中断(除了表格为空的情况)。

假设块的内容是正确的,我想这应该做你所追求的:

if (model.getRowCount() == 0) { 
     //if table is empty, just add 
     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"); 
    }else { 
     //table not empty, look for duplicates first 
     boolean found = false; 
     for (int rowCount = 0 ; rowCount < model.getRowCount(); rowCount++) { 
     // look for duplicate row 
     if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getValueAt(rowCount, 0))) { 
      //found the item : increase total column of existing row 
      found = true; 
      main.tillPanel.lblTotPrice.setText("200"); 
      int currentValue = Integer.parseInt(addSelectedItem[1].trim()); 
      addSelectedItem[1] = "200"; 
      model.setValueAt(addSelectedItem[1], rowCount, 1); 
     }else {//not this row 
     } 
     } 
     if(found == false) { 
     //checked all rows without finding it : add this selection 
     addSelectedItem[1] = "300"; 
     model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]}); 
     main.tillPanel.lblTotPrice.setText("300"); 
     } else { 
     }  
    } 

而且(在不知道的情况下),看行:

INT CurrentValue的=的Integer.parseInt(addSelectedItem [1] .trim()); addSelectedItem [1] =“200”;

是这意味着是:

INT CurrentValue的=的Integer.parseInt(addSelectedItem [1] .trim());addSelectedItem [1] =“”+(currentValue + 100);

目前您解析的这个值在块的末尾被丢弃。
还要注意:

公共静态INT parseInt函数(String s)将 抛出NumberFormatException的

你需要包括周围的任何解析try/catch块