打印收据后清除jtable,再次订购时返回

nr7wwzry  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(150)

当我单击收据按钮时,它会询问用户是否要再次订购,我已经清除了表中的内容,但是当我再次订购新产品时,我在第一张收据上首先订购的项目会返回到表中。。

public void refTable() {

    DefaultTableModel tableModel = (new javax.swing.table.DefaultTableModel(
            new Object[][]{},
            new String[]{
                "Menu", "Price", "Quantity", "Total"
            }
    ) {
        boolean[] canEdit = new boolean[]{
            false, false, false, false
        };

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit[columnIndex];
        }
    });
    try {

        double total = 0;
        for (MenuModel menumodel : controller1.getMenulist()) {
            ArrayList al = new ArrayList();
            al.add(menumodel.getItemName());
            al.add(formatter.format(menumodel.getPrice()));
            al.add((int) menumodel.getQuantity() + "");
            al.add(formatter.format(menumodel.getTotal()));
            total = total + menumodel.getTotal();

            tableModel.addRow(al.toArray());
        }
        double tax = 0.12;
        JLabel_subTotal.setText(formatter.format(total));
        JLabel_Tax.setText(formatter.format(total * tax));
        JLabel_Total.setText(formatter.format((total * tax) + total));
        JTableOrderReceived.setModel(tableModel);
    } catch (NullPointerException e) {
        System.out.println("sad");
    }
}

private void JTableMenuOrderMouseReleased(java.awt.event.MouseEvent evt) {                                              
    // TODO add your handling code here:

    int quanti = 0;
    int row = JTableMenuOrder.rowAtPoint(evt.getPoint());
    if (row >= 0 && row < JTableMenuOrder.getRowCount()) {
        JTableMenuOrder.setRowSelectionInterval(row, row);
    } else {
        JTableMenuOrder.clearSelection();
    }
    int rowindex = JTableMenuOrder.getSelectedRow();
    if (rowindex < 0) {
        return;
    }

    String quantity = JOptionPane.showInputDialog(this, controller.getMenulist().get(rowindex).getItemName() + "\n" + "Number of Quantity :");

    selectedProduct = controller.getMenulist().get(rowindex);
    MenuModel mouseSelected = new MenuModel();
    MenuModel menumodel = controller1.search(selectedProduct.getItemName());

    try {
        if (menumodel != null) {
            selectedProduct = controller1.search(selectedProduct.getItemName());
            if (quanti > 0) {
                double temp = menumodel.getQuantity();
                quanti += temp;
                controller1.removeMenu(selectedProduct);
                refTable();
                mouseSelected.setItemName(selectedProduct.getItemName());
                mouseSelected.setPrice(selectedProduct.getPrice());
                mouseSelected.setQuantity(quanti);
                mouseSelected.setTotal(quanti * selectedProduct.getPrice());
                controller1.addMenu(mouseSelected);
            } else {
                JOptionPane.showMessageDialog(this, "Error");

            }
        } else {
            if (quanti > 0) {
                mouseSelected.setItemName(selectedProduct.getItemName());
                mouseSelected.setPrice(selectedProduct.getPrice());
                mouseSelected.setQuantity(quanti);
                mouseSelected.setTotal(quanti * selectedProduct.getPrice());
                controller1.addMenu(mouseSelected);
            } else {
                JOptionPane.showMessageDialog(this, "Cannot add item: " + selectedProduct.getItemName() + "\nYou must enter quantity", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }

        refTable();

    } catch (NullPointerException e) {
        JOptionPane.showMessageDialog(this, "Cannot add item: " + selectedProduct.getItemName() + "\nQuantity must be greater than zero!", "Error", JOptionPane.ERROR_MESSAGE);
    }
}                                             

private void JTableOrderReceivedMouseReleased(java.awt.event.MouseEvent evt) {                                                  
    // TODO add your handling code here:
    int row = JTableOrderReceived.rowAtPoint(evt.getPoint());
    if (row >= 0 && row < JTableOrderReceived.getRowCount()) {
        JTableOrderReceived.setRowSelectionInterval(row, row);

    } else {
        JTableOrderReceived.clearSelection();
    }

    int rowindex = JTableOrderReceived.getSelectedRow();
    if (rowindex < 0) {
        return;
    }
    selectedProduct = controller.getMenulist().get(rowindex);
    if (evt.isPopupTrigger()) {
        Delete.show(evt.getComponent(), evt.getX(), evt.getY());
    }
}                                                 

  //This is the reciept button action----
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.out.println("Customer name: " + menubill.getCustomerName() + "\n");
    int result = JOptionPane.showConfirmDialog(null, "Would you like to order again?", "EAT N' RUN", JOptionPane.YES_OPTION);
    if (result == JOptionPane.NO_OPTION) {
        System.exit(0);
    }
    DefaultTableModel model = (DefaultTableModel) JTableOrderReceived.getModel();
    model.setRowCount(0);
    JTextField_customerName.setText("");
}

我想问题出在这段代码上(defaulttablemodel=(defaulttablemodel)jtableorderreceived.getmodel();model.setrowcount(0);)它只是暂时清除我收到的订单,但当我再次在菜单中订购产品时,它会返回。。也许有人能帮上忙?或者它怎么了为什么会回来。。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题