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.