public int countComponents()
and public int getComponentCount ()
: These methods are used to get the number of components in the container.
public Component[] getComponents()
: An array of components in the container can be returned using this method. When made any changes to the components, the changes can be seen on the c-screen.
public Component getComponent(int position)
: This method returns the component by pointing to the given position. If the component position is incorrect, the compiler throws ArrayIndexOutOfBoundException.
public Component add(Component component, int position)
: This method is used to add a component at the given position. IllegalArgumentException is thrown if the position is invalid.
public Component add (Component component)
: This method adds the component but as the last object of the container.
public void add (Component component, Object constraints)
: This method is used in layouts, which asks for additional info to accept the components.
public void remove (int index)
: This method removes the component following the index provided. If the index is invalid, compiler throws IllegalArgumentException.
public void remove (Component component)
: This method removes the component in the container without any index or position. If there are nested containers, it simply ignores.
public void removeAll()
: All the components are deleted at a time from the container.
import java.awt.*; import java.applet.*; import java.awt.event.*; public class Splesson extends Applet implements ActionListener { Button b; public void init () { enableEvents (AWTEvent.CONTAINER_EVENT_MASK); add (b = new Button ("One")); add (b = new Button ("Two")); add (b = new Button ("Three")); } protected void processContainerEvent (ContainerEvent e) { if (e.getID() == ContainerEvent.COMPONENT_ADDED) { if (e.getChild() instanceof Button) { Button b = (Button)e.getChild(); b.addActionListener (this); } } } public void actionPerformed (ActionEvent e) { System.out.println ("Selected: " + e.getActionCommand()); } }
Output