简体   繁体   中英

Getting Cell Values in JTable

Anything wrong here?

    JButton btnCheckout = new JButton("Checkout");
    btnCheckout.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            racingUsr.setVisible(false);
            int row = 0;
            int col = 4; 
            int price = 0,subtotal = 0,qty = 0,rowin = table.getSelectedRow();

            if(rowin == 0){
            qty = Integer.parseInt(JOptionPane.showInputDialog(null,"How many of that game?"));
            Object a = table.getModel().getValueAt(row, col);
            a = price;
            subtotal = price * qty;

            JOptionPane.showMessageDialog(null, subtotal);
            }
        }               
    });

Table has 5 rows and 1 column (currently, cant add more yet with this error) coming in from the SQL database. When i enter '5' in the quantity, it outputs '0' in the subtotal. And i also need an extra help for the quantity count, when i buy a game, of course the quantity should decrease but how? I hope someone can help. TIA.

Error:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1

You have a=price; instead of price = a;

This is the reason because you get the 0 in the subtotal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM