FileDocCategorySizeDatePackage
POSFrame.javaAPI DocExample22993Thu Feb 17 20:01:22 GMT 2000UserInterface

POSFrame

public class POSFrame extends JFrame
This is a sample that was based on the JMCU CashSale component built into the Together tool. This presents a simple GUI and shows how we can tie it into the PD classes.
athor
Jon Kern

Fields Summary
ProblemDomain.CashSale
currentSale
ProblemDomain.ProductDesc[]
products
private final String[]
colNames
private static final int
ADDED_DETAIL
private static final int
REMOVED_DETAIL
private static final String[]
cashiers
NumberFormat
currencyFormat
JMenuBar
menuBar1
JMenu
menuFile
JMenuItem
menuFileExit
JMenu
menuHelp
JMenuItem
menuHelpAbout
JToolBar
toolBar
JButton
jButtonHelp
ImageIcon
imageHelp
JLabel
statusBar
BorderLayout
borderLayout1
JScrollPane
jScrollPaneDetails
JTable
jTableSaleDetails
JPanel
jPanel1
BorderLayout
borderLayout2
JPanel
jPanelScan
JButton
jButtonScan
JTextField
jTextFieldItem
JPanel
jPanelDetails
JButton
jButtonNewSale
JButton
jButtonCancel
JPanel
jPanelCompleteSale
JButton
jButtonPayment
JButton
jButtonTotal
BorderLayout
borderLayout3
JTextField
jTextFieldSubTotal
GridLayout
gridLayout1
JLabel
jLabel1
JLabel
jLabel2
JTextField
jTextFieldTax
JLabel
jLabel3
JTextField
jTextFieldPayment
JTextField
jTextFieldTotal
JTextField
jTextFieldChange
JLabel
jLabel4
JLabel
jLabelCashier
JLabel
jLabel6
JLabel
jLabelTimeStamp
JLabel
jLabel8
Constructors Summary
public POSFrame()


    //Construct the frame
      
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); //do not hide the frame, maybe the user will press "Cancel"
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                fileExit_actionPerformed(null);
            }
        });
        try {
            setUpProducts();
            jbInit();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    
Methods Summary
private voidaddDetailToTable()

        int index = currentSale.getDetailList().size() - 1;
        TableDataModel model = (TableDataModel)jTableSaleDetails.getModel();
        model.dataChanged(ADDED_DETAIL, index);
    
private voidcancelSale()

        newSale();

/*
                if ( preCancelCheck() == JOptionPane.OK_OPTION )
                {
                        newSale();
                }
                else
                {
                        // do nothing
                }*/
    
public voidfileExit_actionPerformed(java.awt.event.ActionEvent e)

        if (preCancelCheck() == JOptionPane.OK_OPTION) {
            System.exit(0);
        }
    
public voidhelpAbout_actionPerformed(java.awt.event.ActionEvent e)

        POSFrame_AboutBox dlg = new POSFrame_AboutBox(this);
        Dimension dlgSize = dlg.getPreferredSize();
        Dimension frmSize = getSize();
        Point loc = getLocation();
        dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
        dlg.setModal(true);
        dlg.show();
    
private voidinitButtons()
Keep buttons properly grayed out at the start.

        jButtonNewSale.setEnabled(true);
        jButtonCancel.setEnabled(false);
        jButtonScan.setEnabled(true);
        jButtonPayment.setEnabled(false);
        jButtonTotal.setEnabled(false);
    
private voidinitDetailTable()

        TableDataModel dataModel = new TableDataModel();
        jTableSaleDetails.setModel(dataModel);
        jTableSaleDetails.setPreferredScrollableViewportSize(new Dimension(300, 70));
        TableColumn column = null;
        // {"Item", "Name", "Unit", "Qty", "Price"};
        for (int i = 0; i < jTableSaleDetails.getColumnCount(); ++i) {
            column = jTableSaleDetails.getColumnModel().getColumn(i);
            switch (i) {
                case 0: // Item
                    column.setPreferredWidth(20);
                    break;
                case 1: // Name
                    column.setPreferredWidth(100);
                    break;
                case 2: // Unit
                    column.setPreferredWidth(30);
                    break;
                case 3: // Qty
                    column.setPreferredWidth(20);
                    break;
                case 4: // Price
                    column.setPreferredWidth(30);
                    break;
                default:
                    column.setPreferredWidth(50);
                    break;
            } // ENDSWITCH (col)
        } // ENDFOR (i)
    
voidjButtonCancel_actionPerformed(java.awt.event.ActionEvent e)

        cancelSale();
    
voidjButtonNewSale_actionPerformed(java.awt.event.ActionEvent e)

        newSale();
    
voidjButtonPayment_actionPerformed(java.awt.event.ActionEvent e)

        makePayment();
    
voidjButtonScan_actionPerformed(java.awt.event.ActionEvent e)

        scan();
    
voidjButtonTotal_actionPerformed(java.awt.event.ActionEvent e)

        totalSale();
    
private voidjbInit()

        try {
            imageHelp = new ImageIcon(UserInterface.POSFrame.class.getResource("/UserInterface/help.gif"));
        }
        catch(Exception e) {
        }
        this.getContentPane().setLayout(borderLayout1);
        this.setSize(new Dimension(459, 396));
        this.setTitle("Point Of Sale");
        statusBar.setText(" ");
        menuFile.setText("File");
        menuFileExit.setText("Exit");
        menuFileExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fileExit_actionPerformed(e);
            }
        });
        menuHelp.setText("Help");
        menuHelpAbout.setText("About");
        menuHelpAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                helpAbout_actionPerformed(e);
            }
        });
        if (imageHelp != null)
            jButtonHelp.setIcon(imageHelp);
        jButtonHelp.setToolTipText("Help");
        jPanel1.setLayout(borderLayout2);
        jButtonScan.setToolTipText("Scan next item");
        jButtonScan.setMnemonic('S");
        jButtonScan.setText("Scan");
        jButtonScan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButtonScan_actionPerformed(e);
            }
        });
        jTextFieldItem.setPreferredSize(new Dimension(182, 21));
        jTextFieldItem.setText("<item>");
        jPanelScan.setBorder(BorderFactory.createRaisedBevelBorder());
        jTableSaleDetails.setBorder(BorderFactory.createLineBorder(Color.black));
        jButtonNewSale.setToolTipText("Start a new sale");
        jButtonNewSale.setMnemonic('N");
        jButtonNewSale.setText("New Sale");
        jButtonNewSale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButtonNewSale_actionPerformed(e);
            }
        });
        jButtonCancel.setToolTipText("Cancel current sale");
        jButtonCancel.setMnemonic('A");
        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButtonCancel_actionPerformed(e);
            }
        });
        jButtonPayment.setFont(new java.awt.Font("Dialog", 1, 12));
        jButtonPayment.setToolTipText("Enter payment amount");
        jButtonPayment.setMnemonic('P");
        jButtonPayment.setText("Payment");
        jButtonPayment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButtonPayment_actionPerformed(e);
            }
        });
        jButtonTotal.setFont(new java.awt.Font("Dialog", 1, 12));
        jButtonTotal.setToolTipText("Calculate Total");
        jButtonTotal.setMnemonic('T");
        jButtonTotal.setText("Total");
        jButtonTotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButtonTotal_actionPerformed(e);
            }
        });
        jPanelDetails.setLayout(borderLayout3);
        jTextFieldSubTotal.setText("0.00");
        jTextFieldSubTotal.setHorizontalAlignment(SwingConstants.RIGHT);
        jPanelCompleteSale.setLayout(gridLayout1);
        gridLayout1.setColumns(2);
        gridLayout1.setHgap(3);
        gridLayout1.setRows(5);
        jLabel1.setFont(new java.awt.Font("Dialog", 1, 12));
        jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
        jLabel1.setText("SUBTOTAL:");
        jLabel2.setFont(new java.awt.Font("Dialog", 1, 12));
        jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
        jLabel2.setText("Tax:");
        jLabel3.setText("       ");
        jTextFieldTax.setText("0.00");
        jTextFieldTax.setHorizontalAlignment(SwingConstants.RIGHT);
        jTextFieldPayment.setText("0.00");
        jTextFieldPayment.setHorizontalAlignment(SwingConstants.RIGHT);
        jTextFieldTotal.setText("0.00");
        jTextFieldTotal.setHorizontalAlignment(SwingConstants.RIGHT);
        jTextFieldChange.setText("0.00");
        jTextFieldChange.setHorizontalAlignment(SwingConstants.RIGHT);
        jLabel4.setText("       ");
        jLabelCashier.setText("       ");
        jLabel6.setText("       ");
        jLabelTimeStamp.setText("       ");
        jLabel8.setText("Change:");
        jLabel8.setFont(new java.awt.Font("Dialog", 1, 12));
        jLabel8.setHorizontalAlignment(SwingConstants.RIGHT);
        jScrollPaneDetails.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPaneDetails.setBorder(BorderFactory.createLineBorder(Color.black));
        jPanelDetails.setBackground(Color.yellow);
        toolBar.add(jButtonNewSale);
        toolBar.add(jButtonCancel);
        toolBar.add(jButtonHelp);
        menuFile.add(menuFileExit);
        menuHelp.add(menuHelpAbout);
        menuBar1.add(menuFile);
        menuBar1.add(menuHelp);
        this.setJMenuBar(menuBar1);
        this.getContentPane().add(toolBar, BorderLayout.NORTH);
        this.getContentPane().add(statusBar, BorderLayout.SOUTH);
        this.getContentPane().add(jPanel1, BorderLayout.CENTER);
        jPanel1.add(jPanelScan, BorderLayout.NORTH);
        jPanelScan.add(jButtonScan, null);
        jPanelScan.add(jTextFieldItem, null);
        jPanel1.add(jPanelDetails, BorderLayout.CENTER);
        jPanelDetails.add(jScrollPaneDetails, BorderLayout.NORTH);
        jScrollPaneDetails.getViewport().add(jTableSaleDetails, null);
        jPanel1.add(jPanelCompleteSale, BorderLayout.SOUTH);
        jPanelCompleteSale.add(jLabel3, null);
        jPanelCompleteSale.add(jLabel1, null);
        jPanelCompleteSale.add(jTextFieldSubTotal, null);
        jPanelCompleteSale.add(jLabel4, null);
        jPanelCompleteSale.add(jLabel2, null);
        jPanelCompleteSale.add(jTextFieldTax, null);
        jPanelCompleteSale.add(jLabel6, null);
        jPanelCompleteSale.add(jButtonTotal, null);
        jPanelCompleteSale.add(jTextFieldTotal, null);
        jPanelCompleteSale.add(jLabelCashier, null);
        jPanelCompleteSale.add(jButtonPayment, null);
        jPanelCompleteSale.add(jTextFieldPayment, null);
        jPanelCompleteSale.add(jLabelTimeStamp, null);
        jPanelCompleteSale.add(jLabel8, null);
        jPanelCompleteSale.add(jTextFieldChange, null);
        // Initialize Detail Table
        initDetailTable();
        // Initialize widgets with data
        loadGUIWithPD();
        // Keep buttons properly grayed out at the start
        initButtons();
        this.toolBar.getRootPane().setDefaultButton(jButtonScan);
    
private voidloadGUIWithPD()

        jTextFieldItem.setText("");
        jLabelTimeStamp.setText(DateFormat.getDateInstance().format(currentSale.getTime()));
        jLabelCashier.setText(cashiers[(int)(Math.random() * 10.0)]);
        jTextFieldSubTotal.setText("");
        jTextFieldTax.setText("");
        jTextFieldTotal.setText("");
        jTextFieldPayment.setText("");
        jTextFieldChange.setText("");
        jTextFieldChange.setForeground(Color.black);
    
private voidmakePayment()

        String paymentValue = JOptionPane.showInputDialog("Please enter payment");
        // See if input is valid number
        try {
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMinimumFractionDigits(2);
            double n = numberFormat.parse(paymentValue).doubleValue();
            // Compute change due
            BigDecimal change = currentSale.makeCashSale(new BigDecimal(paymentValue));
            jTextFieldChange.setText(currencyFormat.format(change));
            jTextFieldChange.setForeground(Color.red);
            jTextFieldPayment.setText(currencyFormat.format(new BigDecimal(paymentValue)));
            // Once payment is made, you can no
            // longer cancel or hit payment.
            jButtonPayment.setEnabled(false);
            jButtonCancel.setEnabled(false);
            this.getRootPane().setDefaultButton(jButtonNewSale);
        }
        catch(ParseException pe) {
            JOptionPane.showMessageDialog(null, pe.getMessage(), "Illegal Text Format", JOptionPane.ERROR_MESSAGE);
            makePayment();
        }
        catch(InsuffPaymentException ipe) {
            JOptionPane.showMessageDialog(null, ipe.getMessage(), "Insufficient Payment", JOptionPane.ERROR_MESSAGE);
        }
    
private voidnewSale()
Start off with a fresh CashSale object for the transaction. It will reflect the current timestamp and have no details.

        if (!currentSale.isCompleted()) {
            if (preCancelCheck() != JOptionPane.OK_OPTION) {
                return;
            }
        }
        currentSale = new CashSale();
        TableDataModel model = (TableDataModel)jTableSaleDetails.getModel();
        model.dataChanged(REMOVED_DETAIL, 0);
        // Reset widgets
        loadGUIWithPD();
        initButtons();
    
private intpreCancelCheck()

        if (currentSale.isPending()) {
            return JOptionPane.showConfirmDialog(null, "OK to Cancel Current Sale?", "Sale Pending",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
        }
        else {
            return JOptionPane.OK_OPTION;
        }
    
private voidscan()
Use this to simulate scanning items

        int pseudoIndex = (int)(Math.random() * 10.0);
        ProductDesc prod = products[pseudoIndex];
        jTextFieldItem.setText(prod.getDescription());
        CashSaleDetail detail = new CashSaleDetail(prod);
        // Now add it to the current sale
        currentSale.addSaleDetail(detail);
        // And notify the table
        addDetailToTable();
        jButtonCancel.setEnabled(true);
        jButtonTotal.setEnabled(true);
    
private voidsetUpProducts()

        products = new ProductDesc[10];
        products[0] = new ProductDesc("1", "Pepsi 24-pack", "Pepsi 24", new BigDecimal(3.99));
        products[1] = new ProductDesc("2", "Lays Ridges", "Lays", new BigDecimal(1.99));
        products[2] = new ProductDesc("3", "Vienna Sausages", "Vienna Sausages", new BigDecimal(2.99));
        products[3] = new ProductDesc("4", "White Popcorn", "White Popcorn", new BigDecimal(1.29));
        products[4] = new ProductDesc("5", "Soy Burgers", "Soy Burger", new BigDecimal(5.99));
        products[5] = new ProductDesc("6", "Cat Chow", "Cat Chow", new BigDecimal(9.99));
        products[6] = new ProductDesc("7", "Puppy Chow", "Puppy Chow", new BigDecimal(12.99));
        products[7] = new ProductDesc("8", "Finch Food", "Finch Food", new BigDecimal(1.59));
        products[8] = new ProductDesc("9", "Rice Krispies", "Rice Krispies", new BigDecimal(3.29));
        products[9] = new ProductDesc("10", "Fruit Loops", "Fruit Loops", new BigDecimal(3.49));
    
private voidtotalSale()

        jTextFieldSubTotal.setText(currencyFormat.format(currentSale.calcSubtotal()));
        jTextFieldTax.setText(currencyFormat.format(currentSale.calcTax()));
        jTextFieldTotal.setText(currencyFormat.format(currentSale.calcTotal()));
        // Once you total the sale, you can
        // then make a payment. But we'll disable
        // the ability to scan in more or re-calc
        // total (maybe a bit unrealistic).
        jButtonPayment.setEnabled(true);
        jButtonTotal.setEnabled(false);
        jButtonScan.setEnabled(false);