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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
sgomsConnect With a Mentor Commented:
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
 
heyhey_Commented:
try
parent.doLayout();

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


or even better - use CardLayout
0
All Courses

From novice to tech pro — start learning today.