I am quite new to GUI and Swing in Java, please forgive if I asked an easy question. I am currently working on a project of simple cake ordering GUI. I wanted to create multiple JCheckBox with a loop according to the number of toppings available. The check boxes are displaying just right but I can't add the CheckBox text into my toppingOrder arraylist.
Tried making another String to store the topping.get(i) but it ended up only storing my last item in topping
for (int i = 0; i < topping.size(); i++) {
topping1 = new JCheckBox(topping.get(i));
topping1.setName("topping" + (i+1));
topping1.setFont(standard);
topping1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == 1) {
toppingOrder.add(topping.get(i));
}
}
});
And I am getting this error
Local variable i defined in an enclosing scope must be final or effectively final
Also I am a little confused with
topping1.setName("topping" + (i+1));
I got this from another post but i don't really know the exact function of it. Please help. Thankyou!