0

I have the following for loop:

for (int i=0; i<5; i++){
    if (someVariable==anotherVariable)
        jPanel(i).remove(anElement);
}

I would like to use the for loop to iterate through the JPanels I have, and remove a certain elements from all of them, my jPanels are named jPanel0, jPanel1,jPanel2....jPanel5

I know the above code doesn't work and I have no idea how to do it. Any help would be appreciated

1 Answer 1

2

Instead of creating multiple variables named jPanel0, jPanel1... you should create an array of JPanel[] panels;. Then you can access a specific panel with panels[i] in your loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.