Get child components from a container


****************************************************************
COMPLETE SOURCE CODE FOR : GetChildComponentOfaContainer.java
****************************************************************


import java.awt.Frame;//IMPORT FRAME CLASS INTO THIS PROGRAM
import java.awt.Label;//IMPORT LABEL CLASS INTO THIS PROGRAM
import java.awt.TextField;//IMPORT TEXTFIELD CLASS INTO THIS PROGRAM
import java.awt.Button;//IMPORT BUTTON CLASS INTO THIS PROGRAM
import java.awt.Panel;//IMPORT PANEL CLASS INTO THIS PROGRAM
import java.awt.Component;//IMPORT COMPONENT CLASS INTO THIS PROGRAM

import java.awt.event.WindowListener;//IMPORT WINDOWLISTENER CLASS INTO THIS PROGRAM
import java.awt.event.WindowAdapter;//IMPORT WINDOWADAPTER CLASS INTO THIS PROGRAM
import java.awt.event.WindowEvent;//IMPORT WINDOWEVENT CLASS INTO THIS PROGRAM
import java.awt.event.ActionListener;//IMPORT ACTIONLISTENER CLASS INTO THIS PROGRAM
import java.awt.event.ActionEvent;//IMPORT ACTIONEVENT CLASS INTO THIS PROGRAM

import java.awt.FlowLayout;//IMPORT FLOWLAYOUT CLASS INTO THIS PROGRAM
import java.awt.BorderLayout;//IMPORT BORDERLAYOUT CLASS INTO THIS PROGRAM

import java.awt.Color;//IMPORT COLOR CLASS INTO THIS PROGRAM

import javax.swing.JOptionPane;//IMPORT JOPTIONPANE CLASS INTO THIS PROGRAM

public class GetChildComponentOfaContainer implements ActionListener
{
Frame a=new Frame("GET CHILD COMPONENT IN CONTAINER");
Panel b=new Panel();
Button d=new Button("ADD NEW COMPONENT INTO MONSTER (A LABEL)");
Button c=new Button("CLICK HERE TO SEE NUMBERS OF COMPONENTS IN MONSTER");

public GetChildComponentOfaContainer()
{
//SET LAYOUT FOR FRAME a
a.setLayout(new BorderLayout());

//LISTEN TO WINDOW CLOSING EVENT FOR a
//WHEN SOMEONE CLICK CLOSE BUTTON, THIS APPLICATION WILL TERMINATE.
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

//PANEL b, TO PLACE COMPONENT
b.setBackground(Color.YELLOW);
b.add(new Label("COMPONENT 1 : A LABEL"));
b.add(new TextField("COMPONENT 2 : A TEXTFIELD"));
b.add(new Button("COMPONENT 3 : A BUTTON"));
a.add(b,BorderLayout.CENTER);

//ADD ACTIONLISTENER FOR BUTTON TO MAKE IT FUNCTION WHEN SOMEONE CLICK.
d.addActionListener(this);
c.addActionListener(this);

//PLACE BOTH THE BUTTONS INTO A CONTAINER AND AFTER THAT PLACE THE CONTAINER INTO a
Panel e=new Panel();
e.add(c);
e.add(d);
a.add(e,BorderLayout.SOUTH);

//LABEL AT THE TOP OF a
Label f=new Label("YELLOW AREA IS A PANEL WITH NAME MONSTER");
f.setAlignment(Label.CENTER);
a.add(f,BorderLayout.NORTH);

a.addWindowListener(wl);
a.setSize(800,600);
a.setVisible(true);
}

//ALL BUTTON ACTION IS HERE
public void actionPerformed(ActionEvent evt)
{
//ACTION FOR BUTTON "ADD NEW COMPONENT INTO MONSTER (A LABEL)"
if(evt.getSource()==d)
{
b.add(new Label("NEW"));
//The validate method is used to cause a container to lay out its subcomponents again.
//So you can see the latest label("NEW") that you add.
b.validate();
a.validate();
}

//ACTION FOR BUTTON "CLICK HERE TO SEE NUMBERS OF COMPONENTS IN MONSTER"
if(evt.getSource()==c)
{
Component[]arrayToStoreAllComponent=b.getComponents();//STORE ALL COMPONENT IN b IN AN ARRAY
int numbersOfComponent=arrayToStoreAllComponent.length;//GET ARRAY LENGTH BECAUSE IT SAME TO NUMBERS OF COMPONENT
JOptionPane.showMessageDialog(null,"NUMBERS OF CURRENT COMPONENT IN MONSTER IS : "+numbersOfComponent);
}
}

//MAIN METHOD
public static void main(String[]args)
{
GetChildComponentOfaContainer myObject=new GetChildComponentOfaContainer();
}
}


****************************************************************
JUST COMPILE AND EXECUTE IT
****************************************************************

RELAXING NATURE VIDEO