【问题标题】:Checking for duplicate rows in jTable data检查 jTable 数据中的重复行
【发布时间】:2011-03-08 11:47:28
【问题描述】:

以下是我编写的用于帮助执行以下操作的代码:

从要添加到 jTable 的 jComboBox 中进行选择,然后检查它满足以下三个条件中的哪一个:如果 table 为空,则添加它。如果从组合框中选择的项目存在于表中,则现有行的数量值增加一。或者,如果它还没有在桌子上,它就会被添加。

但我得到的是如下非常奇怪的行为:

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

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

  3. combo 中的第三个选择和上面的 2 一样,所以值是正确的,但应该只有一行

  4. 选择组合框中的第四个选项,这次是为了匹配表中的现有行,而不是更新现有行中的值,它还添加了两个值为“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;
        }
    }
});

【问题讨论】:

    标签: java swing jtable jcombobox


    【解决方案1】:

    查看代码我强烈怀疑问题在于中断,它们正在将您的 for 循环变成 if-else 设置。它们是您只查看第一行的原因,我怀疑您实际上打算使用 continue 而不是 break(表为空的情况除外)。

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

        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);

    ?

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

    public static int parseInt(String s) 抛出 NumberFormatException

    您需要在任何解析周围包含 try/catch 块

    【讨论】:

      【解决方案2】:

      编辑:等等,为什么在每个 if 或 else 中都有 break 语句。您的循环只会运行一次。因此,如果它没有找到第一次传递的值,它将退出。

      取出 break 语句。还将 if 语句以检查表是否为空到循环之外。

      【讨论】:

        【解决方案3】:

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

        我猜你是说

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

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

        编辑:

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

        您不查找重复行,而是检查 first 行是否重复。如果不是,请再次添加相同的项目(使用“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")
           }     
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-26
          • 2020-03-22
          • 1970-01-01
          • 1970-01-01
          • 2015-07-02
          • 2018-05-16
          • 2018-12-11
          • 2021-01-16
          相关资源
          最近更新 更多