Modifying and reloading JPanel component
I am relatively new to Java, been taking a class for a year now, and I'm currently working on a Mastermind game. I've made it quite fancy and way too complex for my skills, so I've run into a snag.
I have created JLabels setting a specific icon image for each of the bullets telling users if their selection was right color in right spot or right color. These are all given a specific grid name. When the user presses the "Guess" button, I dynamically create this specific grid value and then try to replace the existing one. I've tried a number of methods including just resetting the icon using the generated name or by cycling through the panel's components. Neither is successful.
Can anyone offer some suggestions on how to fix this problem? I have both of my solutions here, so it's a little mucked up.
private void displayGridAnswers(int numBlackDots, int numRedDots)
for (int i=1; i<=numBlackDots; i++)
String label = "grid_" + selectedRow + "_answer_" + i;
JLabel gridAnswer = new JLabel(label);
Component components = gamePanel.getComponents();
Component component = null;
for (int j = 0; j < components.length; j++)
component = components[j];
if (component instanceof JLabel)
String string = component.toString();
if (string.indexOf(label) != -1)
String newString = string.replace("grey", "ring");
components[j] = new JLabel(newString);
// components[j] = gridAnswer;
} // end if
} // end for
// Repaint the game panel
} // end method