Unable to remove a child panel and see the new child panel (the view does not refresh)

please could you send me a code solution which will make the following code work.

In this example i am unable to see the new child panel, the screen does not refresh even though the code seems to be executed.

Panel parent = new Panel();
parent.setLayout(new BorderLayout());
Panel child = new Panel();
parent.add("Center", child);

parent.getLayout().removeLayoutComponent(child);
child = new Panel();
parent.add("Center", child);

help_meAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

heyhey_Commented:
try
parent.doLayout();

or
child.invalidate();
parent.validate();


or even better - use CardLayout
0
sgomsCommented:
public void removeLayoutComponent(Component comp)

Removes the specified component from this border layout. This method is called when a container calls its remove or removeAll methods. Most applications do not call this method directly.

____________________________________________________________
so instead use,
Panel parent = new Panel();
parent.setLayout(new BorderLayout());
Panel child = new Panel();
parent.add("Center", child);
parent.remove(child);
child = new Panel();
parent.add("Center", child);
validate(); //very important that u validate teh alyout after                 //reaaanging the components!

____________________________________________________________
//a sample program , removeChildPanel.java
import java.awt.*;
import java.awt.event.*;

class removeChildPanel extends Frame{
   Panel parent,child;
   removeChildPanel(){
              setTitle("Child Panel");
                  setSize(300,300);
                  parent = new Panel();
      parent.setLayout(new BorderLayout());
      child = new Panel();
                  child.setBackground(Color.blue);
      parent.add("Center", child);
      Button change=new Button("Change");
                  change.addActionListener(new ActionListener(){
                     public void actionPerformed(ActionEvent e){
                               parent.remove(child);
                        child = new Panel();
                               child.setBackground(Color.green);
                        parent.add("Center", child);
                               validate();
                         }
                  });
                  
                  add("Center",parent);
                  add("South",change);
       }
       public static void main(String args[]){
             (new removeChildPanel()).setVisible(true);
        }
}


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.