AWT - SPLessons

AWT Add Components to a Container

Home > Lesson > Chapter 10
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

AWT Add Components to a Container

AWT Add Components to a Container

shape Introduction

The current chapter explains how to add components to a container. The following concepts are covered in "AWT Add Components to a Container" chapter:
  • Identifying Position
  • Adding Components
  • Deleting Components

shape Description

AWT Add Components to a Container to make it as an application and make it viewable on a window.

Checking

shape Methods

Few methods that are used to count the components and set position into a container are given below: 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.

Adding

shape Methods

Some of the methods used to add the components to the container are given below: 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.

Removing

shape Methods

Some of the methods used to remove the components from the container are given below: 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.

shape Conceptual figure

shape Examples

[java] 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()); } } [/java] Output

Summary

shape Key Points

  • getComponent() method identifies the components.
  • add() method includes or adds the components in the container.
  • remove() method deletes the components.