【问题标题】:jCombobox giving incremental value error on jTablejCombobox在jTable上给出增量值错误
【发布时间】:2011-03-11 10:46:18
【问题描述】:

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

还有用于检查表上重复条目的 alos 代码。如果发现重复条目​​,则应将 qty 列增加一,而不是创建单独的条目。

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

我还包含了根据从表中选择的 Round Drop 填充 Round Details 的代码,以供参考,但我相当确定问题出在下面的代码中。导航如下...

要进入下面的屏幕... Round Drop panel table of round drop) >> 单击表格行并转到相关的回合详细信息面板 >> 按 Till 按钮将用户带到屏幕,代码如下...

测试结果:

使用上面的导航首先通过下面的代码会得到预期的结果

第二遍给出初始值 2(而不是 1),重复行将 qty 增加 2 而不是 1

第三遍给出初始值 3(而不是 1),重复行将 qty 增加 3 而不是 1

第四遍给出初始值 4(而不是 1),重复行将 qty 增加 4 而不是 1

...等等。

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

谢谢

/************ *代码示例******** *************** *********/ 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] = "";
            }
        }
    });

//此代码加载特定的回合详细信息,基于回合掉落表的选择

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

【问题讨论】:

    标签: loops jtable jcombobox


    【解决方案1】:

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

    【讨论】:

    • 不确定这会有什么帮助,好像这是第一个选择也会触发两次的问题,不是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    相关资源
    最近更新 更多