I have multiple JSpinner which have items 1-10 , and one JTable. When the JSpinner is clicked, the value will be added to JTable. I want to get the row number so I can use it at setValueAt. But I get error when the JSpinner clicked more than once times.

Code

    public void stateChanged(ChangeEvent e) {  // when JSpinner clicked
            int quantity = (int) ((JSpinner) e.getSource()).getValue();
            int rows = table.getRowCount();

            for (int i = 0; i < ELEMENTS; i++) {
                if (numspinner[i] == e.getSource()) {
                    if (quantity == 1) {
                        System.out.println("Row"+rows);
                        dtm.addRow(new Object[] { foodLabel[i].getText(), quantity, price[i] * quantity });
                    } else {
                        System.out.println("Row"+rows);
                        dtm.setValueAt(quantity, rows, 3); 
                    }
                }
            }
        }

I clicked the same JSpinner more then once and get this output

Row1
Row2

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
        at java.util.Vector.elementAt(Unknown Source)
        at javax.swing.table.DefaultTableModel.setValueAt(Unknown Source)
        at gui.FoodOrdering.stateChanged(FoodOrdering.java:250)
        at javax.swing.JSpinner.fireStateChanged(Unknown Source)
        at javax.swing.JSpinner$ModelListener.stateChanged(Unknown Source)
        at javax.swing.AbstractSpinnerModel.fireStateChanged(Unknown Source)
        at javax.swing.SpinnerNumberModel.setValue(Unknown Source)
        at javax.swing.JSpinner.setValue(Unknown Source)
        at javax.swing.plaf.basic.BasicSpinnerUI$ArrowButtonHandler.actionPerformed(Unknown Source)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

Any help would be appreciated.

Recommended Answers

All 7 Replies

Just a guess:
Its line 12 that's the problem. rows is the number of rows, so valid values for the second parameter should be 0 to (rows-1)

Just a guess:
Its line 12 that's the problem. rows is the number of rows, so valid values for the second parameter should be 0 to (rows-1)

Yes, the error is pointed to line 12. How can I solve that ? I want to change the value in the specific row

I don't know your design, so I don't know wha the "specific row" is.
What I can say is that if rows is (eg) 2, then the valid values for the second parameter are 0 and 1, but not 2. ie, the first row is row 0 and the second row is row 1, there is no row 2.

I don't know your design, so I don't know wha the "specific row" is.
What I can say is that if rows is (eg) 2, then the valid values for the second parameter are 0 and 1, but not 2. ie, the first row is row 0 and the second row is row 1, there is no row 2.

Can you explain why I will get Row1 and Row2 when I click the JSpinner more than once ?

I want to change the Noodles quantity to 2, but it display errors.

Attach with my image screen shot

If quantity is 1 then you add a new row, and do not execute line 12. For other quantity values you execute line 12 with the second parameter invalid (see above)

But how can I get the rows number so the second parameter become valid ?? Was pulling my hair out of this !

I have move some code and here the latest

public void stateChanged(ChangeEvent e) {
        int quantity = (int) ((JSpinner) e.getSource()).getValue();
        int rows = 0;

        for (int i = 0; i < ELEMENTS; i++) {
            if (numspinner[i] == e.getSource()) {
                if (quantity == 1) {
                    System.out.println("Row"+rows);
                    rows = table.getRowCount();
                    dtm.addRow(new Object[] { foodLabel[i].getText(), quantity, price[i] * quantity });
                } else {
                    System.out.println(" The Row"+rows);
                    dtm.setValueAt(quantity, rows, 1); // obj,column,row
                }
            }
        }   
}

If the quantity is 1, it adds row as expected. When click the same JSpinner again, it always display 0

If quantity is not 1 the you don't set rows properly and it's left at 0 (only done on line 9).
If you are trying to update the last row of 'n' rows then the row number is n-1.
I don't know which row you are trying to update but just remember first row is row 0 and the second row is row 1(etc)

I don't know how to make that any clearer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.