jCombobox 在 jTable 上给出增量值错误

发布于 2024-10-21 17:47:39 字数 6319 浏览 6 评论 0原文

我的下面的代码有问题,下面的代码显示了一个正在填充的 jComboBox,当我从此列表中选择一个项目时,它会添加到其下面的 jTable 中。

还有一个代码可以检查表上是否有重复的条目。如果发现重复条目​​,则应将数量列加一,而不是创建单独的条目。

这就是问题所在,当我按下此屏幕上的后退按钮并转到不同的屏幕,然后通过与第一次相同的路线返回时,我会在表行/单元格中添加逐渐不同的数量。

我还包含了根据从表中选择的回合掉落填充回合详细信息的代码,以供参考,但我相当确定问题出在下面的代码中。导航如下...

到达下面的屏幕... Round Drop 面板(圆形水滴表)>>>单击表格行并转到关联的回合详细信息面板>>按下 Till 按钮会将用户带到带有下面代码的屏幕...

测试结果:

使用上面的导航第一次通过下面的代码给出预期的结果

第二次给出初始值 2(而不是 1),并且重复行将数量增加 2而不是一次

第三遍给出初始值 3(而不是 1),并且重复行将数量增加 3 而不是一次

第四遍给出初始值 4(而不是 1),并且重复行将数量增加 4 而不是一个

……等等。

任何有关解决方案或更好设计的帮助、指导将不胜感激。

谢谢

/*************< /em>代码示例********************************/ public voidtilOperations(String sourceCall) {

    final DefaultTableModel model = (DefaultTableModel)main.tillPanel.tblTillSale.getModel();
    if (main.tillPanel.cmbTillProdSelect.getItemCount() < 1) {
        for (int d = 0; d < roundStockObj.length ; d++) {
            main.tillPanel.cmbTillProdSelect.addItem(roundStockObj[d].getDescription());
        }}
    main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);

    main.tillPanel.cmbTillProdSelect.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent f)
        {
            int qty = 1;
            for (int index = 0; index < 4; index++) {
                addSelectedItem[index] = "";
            }
            int row;
            selectedItem = null;
            main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
            selectedItem = main.tillPanel.cmbTillProdSelect.getSelectedItem();

            for (int d = 0; d < roundStockObj.length; d++) {

                if (selectedItem.equals(roundStockObj[d].getDescription())) {    
                    addSelectedItem[0] = roundStockObj[d].getDescription();
                    addSelectedItem[1] = Integer.toString(qty);
                    addSelectedItem[2] = Double.toString(roundStockObj[d].getPrice()).trim();
                    addSelectedItem[3] = Double.toString(roundStockObj[d].getPrice()).trim();
                    //break;
                }
            }

            if(model.getRowCount() == 0) { //check if model is empty
                model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
            }
            else { //check if there is a duplicate row
                int duplicateRow = -1;
                for (row = 0 ; row < model.getRowCount(); row++) {
                    if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getModel().getValueAt(row,0))) {
                        duplicateRow = row;
                        break;
                    }
                }

                if(duplicateRow == -1) { //if there is no duplicate row, append
                    model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
                }
                else { //if there is a duplicate row, update
                    main.tillPanel.jLabel1.setText(addSelectedItem[1]);
                    DecimalFormat fmtObj = new DecimalFormat("####0.00");
                    int currentValue = Integer.parseInt(main.tillPanel.tblTillSale.getValueAt(row, 1).toString().trim());
                    int newValue = currentValue + 1;
                    Integer newValueInt = new Integer(newValue);
                    model.setValueAt(newValueInt, row, 1);

                    double unitPrice = Double.parseDouble(main.tillPanel.tblTillSale.getValueAt(row, 2).toString().trim());
                    double newPrice = newValue * unitPrice;
                    Double newPriceDbl = new Double(newPrice);
                    main.tillPanel.tblTillSale.setValueAt(fmtObj.format(newPriceDbl), row, 3);
                }
            }

            main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
            for (int index = 0; index < 4; index++) {
                addSelectedItem[index] = "";
            }
        }
    });

//此代码根据回合丢弃表中的选择加载特定的回合详细信息

public void displayRoundDropDetails() {
    DefaultTableModel model = (DefaultTableModel)main.selectRoundDropPanel.tblSelectRoundDrop.getModel();

    if (!loaded) {
    for (int d = 0; d < roundDropsData.length; d++) {
        if (roundDropsData[d][0].equals(defaultRoundID)) {
            model.addRow(new Object[]{roundDropsData[d][3], roundDropsData[d][2],
                    roundDropsData[d][4], roundDropsData[d][5]});
        }
    }
    loaded = true;
    }
    main.selectRoundDropPanel.tblSelectRoundDrop.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent evt)
        {
            int row = 0;
            row = main.selectRoundDropPanel.tblSelectRoundDrop.getSelectedRow();
            for (int index = 0; index < roundDropsData.length; index++) {
                if (roundDropsData[index][3].equals(
                        main.selectRoundDropPanel.tblSelectRoundDrop.getModel().getValueAt(row, 0))) {

                    main.roundDetailsPanel.txtRoundDetailsAddress.setText(roundDropsData[index][6] + "\n"
                            + roundDropsData[index][7] + ", " + roundDropsData[index][8] + "\n" +
                            roundDropsData[index][9]);

                    main.roundDetailsPanel.lblRoundDetailsName.setText(roundDropsData[index][2]);
                    main.roundDetailsPanel.txtRoundDetailsInstuct.setText(roundDropsData[index][10]);
                    main.roundDetailsPanel.txtDropDetailsIn.setText(roundDropsData[index][4]);
                    main.roundDetailsPanel.txtDropDetailsOut.setText(roundDropsData[index][5]);
                    main.roundDetailsPanel.txtRoundDetailsInstruct.setText(roundDropsData[index][12]);
                    break;
                }
            }
        Globals.CURRENT_COMPONENT = "selectRoundDropPanel";
        showRoundDetailsPanel();
        }
    });
}

I am having problems with my code below, the code below shows a jComboBox being populated, when i select an item from this list it is added to the jTable below it.

There is alos code to check for duplicate entries ont he table. If a duplicate entry is found it should increase the qty column by one and not create a seperate entry.

This is where the problem comes in, when I press the back button on this screen and go to a different screen and then come back via same route as the first time, I get an incrementally different qty added to the table row/cell.

I have also included the code that populates the Round Details depending on Round Drop selected from table, for reference, but Im fairly certain the problem lies in the below code. The navigation is as follows...

To get to the below screen... Round Drop panel table of round drops) >> click on table row and taken to associated round details panel >> pressing the Till button takes user to screen with code below...

Test results:

First pass through below code using navigation above gives results as expected

Second pass gives an initial value of 2 (instead of one), and duplicate row increases qty by 2 instead of one

Third pass gives an initial value of 3 (instead of one), and duplicate row increases qty by 3 instead of one

Fourth pass gives an initial value of 4 (instead of one), and duplicate row increases qty by 4 instead of one

...and so on.

Any help, guidance on solution or a better design would be hugely appreciated.

Thanks

/*************Code sample ********************************/
public void tillOperations(String sourceCall) {

    final DefaultTableModel model = (DefaultTableModel)main.tillPanel.tblTillSale.getModel();
    if (main.tillPanel.cmbTillProdSelect.getItemCount() < 1) {
        for (int d = 0; d < roundStockObj.length ; d++) {
            main.tillPanel.cmbTillProdSelect.addItem(roundStockObj[d].getDescription());
        }}
    main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);

    main.tillPanel.cmbTillProdSelect.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent f)
        {
            int qty = 1;
            for (int index = 0; index < 4; index++) {
                addSelectedItem[index] = "";
            }
            int row;
            selectedItem = null;
            main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
            selectedItem = main.tillPanel.cmbTillProdSelect.getSelectedItem();

            for (int d = 0; d < roundStockObj.length; d++) {

                if (selectedItem.equals(roundStockObj[d].getDescription())) {    
                    addSelectedItem[0] = roundStockObj[d].getDescription();
                    addSelectedItem[1] = Integer.toString(qty);
                    addSelectedItem[2] = Double.toString(roundStockObj[d].getPrice()).trim();
                    addSelectedItem[3] = Double.toString(roundStockObj[d].getPrice()).trim();
                    //break;
                }
            }

            if(model.getRowCount() == 0) { //check if model is empty
                model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
            }
            else { //check if there is a duplicate row
                int duplicateRow = -1;
                for (row = 0 ; row < model.getRowCount(); row++) {
                    if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getModel().getValueAt(row,0))) {
                        duplicateRow = row;
                        break;
                    }
                }

                if(duplicateRow == -1) { //if there is no duplicate row, append
                    model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
                }
                else { //if there is a duplicate row, update
                    main.tillPanel.jLabel1.setText(addSelectedItem[1]);
                    DecimalFormat fmtObj = new DecimalFormat("####0.00");
                    int currentValue = Integer.parseInt(main.tillPanel.tblTillSale.getValueAt(row, 1).toString().trim());
                    int newValue = currentValue + 1;
                    Integer newValueInt = new Integer(newValue);
                    model.setValueAt(newValueInt, row, 1);

                    double unitPrice = Double.parseDouble(main.tillPanel.tblTillSale.getValueAt(row, 2).toString().trim());
                    double newPrice = newValue * unitPrice;
                    Double newPriceDbl = new Double(newPrice);
                    main.tillPanel.tblTillSale.setValueAt(fmtObj.format(newPriceDbl), row, 3);
                }
            }

            main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount() - 1);
            for (int index = 0; index < 4; index++) {
                addSelectedItem[index] = "";
            }
        }
    });

//This code loads the specific Round Details, based on the selection form the round drops table

public void displayRoundDropDetails() {
    DefaultTableModel model = (DefaultTableModel)main.selectRoundDropPanel.tblSelectRoundDrop.getModel();

    if (!loaded) {
    for (int d = 0; d < roundDropsData.length; d++) {
        if (roundDropsData[d][0].equals(defaultRoundID)) {
            model.addRow(new Object[]{roundDropsData[d][3], roundDropsData[d][2],
                    roundDropsData[d][4], roundDropsData[d][5]});
        }
    }
    loaded = true;
    }
    main.selectRoundDropPanel.tblSelectRoundDrop.addMouseListener(new MouseAdapter()
    {
        public void mouseClicked(MouseEvent evt)
        {
            int row = 0;
            row = main.selectRoundDropPanel.tblSelectRoundDrop.getSelectedRow();
            for (int index = 0; index < roundDropsData.length; index++) {
                if (roundDropsData[index][3].equals(
                        main.selectRoundDropPanel.tblSelectRoundDrop.getModel().getValueAt(row, 0))) {

                    main.roundDetailsPanel.txtRoundDetailsAddress.setText(roundDropsData[index][6] + "\n"
                            + roundDropsData[index][7] + ", " + roundDropsData[index][8] + "\n" +
                            roundDropsData[index][9]);

                    main.roundDetailsPanel.lblRoundDetailsName.setText(roundDropsData[index][2]);
                    main.roundDetailsPanel.txtRoundDetailsInstuct.setText(roundDropsData[index][10]);
                    main.roundDetailsPanel.txtDropDetailsIn.setText(roundDropsData[index][4]);
                    main.roundDetailsPanel.txtDropDetailsOut.setText(roundDropsData[index][5]);
                    main.roundDetailsPanel.txtRoundDetailsInstruct.setText(roundDropsData[index][12]);
                    break;
                }
            }
        Globals.CURRENT_COMPONENT = "selectRoundDropPanel";
        showRoundDetailsPanel();
        }
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花肆意 2024-10-28 17:47:39

尝试更改 JComboBox 的侦听器。尝试使用 stateChangeListener。

Try changing the listener for JComboBox. try using stateChangeListener.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文