I am trying to add buttons to a panel that is a panel array. For some reason, each panel seems to hav the same buttons added to them. I thought using the iteration variable would keep the same 36 buttons from being added to each panel, but it seemed to fail. Any help with figuring out how to fix this would be greatly appreciated.




public static void listAll(Boolean bool) {
//Initializes all panels in the panels[] array
for (int i = 0; i < numberOfPanels; i++) {
panels[i] = new JPanel();
panels[i].add(Test.moreButtons[0]);
}
//Initializes all the buttons in the butonsForward[] and buttonsBackward arrays
for (int i = 0; i < 30; i++) {
buttonsForward[i] = new Button("Next Page");
buttonsForward[i].addActionListener(new PageButtonActionListener());
buttonsForward[i].setBounds(200, 240, 100, 20);
buttonsBackward[i] = new Button("Last Page");
buttonsBackward[i]
.addActionListener(new PageButtonActionListener());
buttonsBackward[i].setBounds(125, 240, 75, 20);
}
// Adds forward buttons to all panels in array
for (int i = 0; i < (numberOfPanels - 1); i++) {
panels[i].add(buttonsForward[i]);
}
// Adds backward buttons to all panels in array
for (int i = 1; i < numberOfPanels; i++) {
panels[i].add(buttonsBackward[i - 1]);
}
//Check to see if method is called
System.out.println("Listing all files");
Test.listOfFiles = Test.folder.listFiles();
//Checks to see how many files there are
int filecheck = Test.listOfFiles.length;
if (filecheck == 0) {
Test.mainScreen();
if (bool == true) {
Lib.noEntries();
}
}
//This works fine
if (filecheck <= 36) {
Test.buttons = new Button[Test.listOfFiles.length];
int k = 0;
int j = 0;
for (int i = 0; i < Test.listOfFiles.length; i++) {
if (k >= 12) {
k = k - 12;
j += 1;
}
Test.buttons[i] = new Button(
Lib.convertFile(Test.listOfFiles[i].getName()));
Test.panel.add(Test.buttons[i]);
Test.buttons[i].setBounds((100 * j), 20 * k, 100, 20);
Test.buttons[i]
.addActionListener(new NamedButtonActionListener());
k++;
}
Test.panel.add(Test.moreButtons[0]);
}
//This is where all the panels in the array have the same buttons
if (filecheck > 36) {
Test.frame.remove(Test.panel);
Test.buttons = new Button[Test.listOfFiles.length];
for (int i = 0; i < numberOfPanels; i++) {
int iteration = 0;
int x = 0;
int y = 0;
for (int j = 0; j < 36; j++) {
if (x >= 12) {
x = x - 12;
y += 1;
}
Test.buttons[iteration + j] = new Button(
Lib.convertFile(Test.listOfFiles[iteration + j]
.getName()));
panels[i].add(Test.buttons[iteration + j]);
Test.buttons[iteration + j].setBounds((100 * y), 20 * x,
100, 20);
Test.buttons[iteration + j]
.addActionListener(new NamedButtonActionListener());
x++;
}
iteration += 36;
x = 0;
y = 0;
}
Test.frame.add(panels[0]);
panels[0].add(Test.moreButtons[0]);
}
}

--- Update ---

Fixed it by putting the int iteration outside of the bigger for loop. And adding another int to keep track of button numbers.