Ali Ben Messaoud
Ali Ben Messaoud

Reputation: 11920

Add JPanel in arraylist<JPanel>

I have a JPanel called parentPanel. Also I have other sonPanels in parentPanel.

I want to remove the sonPanels and add them in an arraylist<JPanel>

Can you help me??

Thanks in advance!!! :)

Upvotes: 1

Views: 1135

Answers (1)

Kaushik Shankar
Kaushik Shankar

Reputation: 5619

Since JPanel inherits from Container, you can use the getComponents() method to get the list of your sonPanels.

After getting them all, you can clear you parentPanel by calling the removeAll() method. If you had a deleteRows method, simply call the first method on your JPanel, lets call it contentPane, and then call the second method to remove.

public Component[] getAndClearSonPanels() {
    Component[] currentComponents = contentPane.getComponents();
    contentPane.removeAll();
    return currentComponents;
}

If you need to traverse even more deeply into each of the JPanels, you would need to, recursively do so.

Upvotes: 2

Related Questions