Reputation: 6657
I have JPanel has already added to JFrame. And I have dynamically added JPanel. After adding JPanel on the fly it stay near the JPanel added before. How can I delete previous JPanel?
PS I also think about using cardlayout - is it a good way?
Upvotes: 1
Views: 1030
Reputation: 37729
There is a method getComponents()
which will give you the child component in JFrame
you will use it like this way:
Component[] comp = frame.getContentPane().getComponents();
for(int i=0; i<comp.lenght; i++)
{
if(comp[i] instanceof JPanel)
{
frame.remove(comp[i]);
}
}
Note: this fix only works when you have single JPanel
inside JFrame
at a time, otherwise you gonna remove all your JPanels
from JFrame
.
Upvotes: 1