0

I currently have a shopping cart that records how many orders have been made by using a :

int orderNumber and just ++ when ever a new order is made, I want to add a edit function where someone will click on a edit button and it will open a new frame where they will select what order they want to edit via a combobox, the problem is that I need the comboBox to fill with 1,2,3 etc... Depending on how many orders were made. I had a go at achieving this with the method below however it only errors.

orderNumbersList = new String[orderNumber];
                for (int i = 1; i <= orderNumber; i++) {
                    orderNumbersList[i] = "" + i;
                }

             JComboBox orderNumberBox = new JComboBox(orderNumbersList);
Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
  • Adapt the answer to http://stackoverflow.com/questions/9344708/jcombobox-to-list-age to your purposes. – kiheru Jul 25 '13 at 10:16

1 Answers1

2

Arrays are zero based. Replace

for (int i = 1; i <= orderNumber; i++) {
   orderNumbersList[i] = "" + i;
}

with

for (int i = 0; i < orderNumber; i++) {
    orderNumbersList[i] = Integer.toString(i + 1);
}

Notice the assigned value i is adjusted

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • that has made it work thanks, but the problem is now though that it starts of with a order number of 0 where as obviously my order numbers start of with one . – user2613957 Jul 25 '13 at 09:59