josh reilly
josh reilly

Reputation: 53

removing multiple buttons with the same name

Im adding buttons for a game but when removing the button in a loop it will only get rid of one button even though i added them in the same way

for(int i=0 - 1; i < 4 ; i++) {
    panelButtonsub.remove(buttonBlankItems);
    }
    panelButtonsub.setLayout (new GridLayout (intLayout,intLayout));
    revalidate();
    repaint();

Upvotes: 0

Views: 102

Answers (2)

camickr
camickr

Reputation: 324207

If you want to remove all the buttons in the panel you can use:

panel.removeAll();

If you want to remove the first 4 buttons in the panel you can use:

for (int i = 0; i < 4, i++)
    panel.remove(0);

If you are trying to remove a certain type of component from the panel then you need to start at the end to remove the components:

int components = panel.getComponentCount();

for (int i = components - 1; i >= 0; i --)
{
    Component c = panel.getComponent(i);

    if (c instance of BlankButton)
        panel.remove(i);
}

Where BlankButton is the component you created to represent the extra space by using panel.add( new BlankButton(...) ).

If you are trying to do something else then you need to clarify your question.

Upvotes: 2

alex.pulver
alex.pulver

Reputation: 2113

You must have distinct instances for each buttonBlankItems button. I guess that you are adding the same button 5 times and then you are trying to remove them.

Upvotes: -1

Related Questions