0

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!

2 Answers2

1

This snippet here:

topping1.setName("topping" + (i+1));

will simply setthe name of the checkbox by combining the string "topping" with the number (e.g. "topping1").

The issue you are having is due to scope and visibility. You are trying to access your local variable from within an anonymous inner class. You could check this answer for more information on how to solve this.

As a quick fix, what you could do is duplicate your counter and reassign it on every run:

    for (int i = 0; i < topping.size(); i++) {
        final int temp = i;
        topping1.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == 1) {
                    toppingOrder.add(topping.get(temp));
                }
            }
        });
    }

I think this would work for you.

maloomeister
  • 2,461
  • 1
  • 12
  • 21
  • this works perfectly, thank you so much. Its weird that i have tried declaring a final int globally to do similar job, but Eclipse told me i need to initialized it first. Silly me for not trying to declare it inside the for loop. Once again thanks! – zhthemagician Aug 03 '20 at 08:17
-2
for (final int i = 0; i < topping.size(); i++)

and for topping1.setName you are just setting its name to "topping1" when i = 0 and "topping2" when i = 1 and so on and so forth.

  • "The final local variable i cannot be assigned. It must be blank and not using a compound assignment", it is showing this error now. From as far as i know final can only be instantiated one time or so. – zhthemagician Aug 03 '20 at 08:05
  • And how can i call and get text from, say topping2 with the current value of i in the ItemListener? – zhthemagician Aug 03 '20 at 08:07
  • You can't use a final int as a counter in a for loop. You do you think the incrementation would work? – maloomeister Aug 03 '20 at 08:09