Removing JButtons of certain names from a JPanel

GooseWithDaGibus :

I have a JPanel called tilesLayer, on this panel I use a loop to make a grid of JButtons with their names set to the coordinates like this: X,Y. I need to remove a number of these buttons in a loop. I have a listener set up for when it's a key is pressed it will remove a row. I tried to use the .remove(); method. But I can't seem to find the syntax that will allow me to remove a button called 1,2 or 1,3 or 1,3 and so on. Whats the proper syntax for doing that?

Here's what I figure loop would look like: cullX is determined earlier in the code, its 48 to be exact. xCord and yCord are already know before hand as well. But obviously the syntax I'm using isn't correct the remove method, and that's what I need to figure out.

 while (buttonsRemoved <= cullX) {
        tileLayer.remove(xCord + "," + yCord);
Abra :
  1. Get the list of components in tileLayer.
  2. Iterate through the list.
  3. For each component, check if it is a JButton.
  4. If it is, check if its text is the text you are looking for.
  5. If it is, remove it from tileLayer

Code implementing above algorithm:

java.awt.Component[] components = tileLayer.getComponents();
for (java.awt.Component component : components) {
    if (component instanceof javax.swing.JButton) {
        javax.swing.JButton button = (javax.swing.JButton) component;
        String text = button.getText();
        if (text.equals(xCord + "," + yCord) {

Guess you like