Determine when a component is added or removed from a container


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


import java.awt.Frame;//import Frame class into program
import java.awt.Panel;//import Panel class into program
import java.awt.Button;//import Button class into program
import java.awt.Label;//import Label class into program
import java.awt.Component;//import Component class into program

import java.awt.event.ContainerListener;//import ContainerListener class into program
import java.awt.event.ContainerAdapter;//import ContainerAdapter class into program
import java.awt.event.ContainerEvent;//import ContainerEvent class into program

import java.awt.event.WindowListener;//import WindowListener class into program
import java.awt.event.WindowAdapter;//import WindowAdapter class into program
import java.awt.event.WindowEvent;//import WindowEvent class into program

import java.awt.event.ActionListener;//import ActionListener class into program
import java.awt.event.ActionEvent;//import ActionEvent class into program

import java.awt.BorderLayout;//import BorderLayout class into program
import java.awt.FlowLayout;//import FlowLayout class into program

import java.awt.Color;//import Color class into program

import javax.swing.JOptionPane;//import JOptionPane class into program

public class DetermineWhenAComponentIsAddOrRemove implements ActionListener
{
//Initiate Frame(a) object
Frame a=new Frame("DETERMINE WHEN A COMPONENT IS ADD OR REMOVE");
//Initiate Button(b) object
Button b=new Button("CLICK HERE TO ADD NEW COMPONENT(A LABEL)");
//Initiate Button(d) object
Button d=new Button("CLICK HERE TO REMOVE NEW INSERTED COMPONENT");
//Initiate Panel(c) object
Panel c=new Panel();

public DetermineWhenAComponentIsAddOrRemove()
{
//Set layout for Frame(a)
a.setLayout(new BorderLayout());

//Listen when someone click at Frame(a) close button
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

//Listen when a component is added or removed from Panel(c)
ContainerListener cl=new ContainerAdapter()
{
public void componentAdded(ContainerEvent evt)
{
JOptionPane.showMessageDialog(null,"A COMPONENT ADDED");
}

public void componentRemoved(ContainerEvent evt)
{
JOptionPane.showMessageDialog(null,"A COMPONENT REMOVED");
}
};

//Label at the top of Frame(a)
Label e=new Label("PANEL WITH BLUE COLOR AT THE CENTER IS A CONTAINER");
//Make Label's allignment to center
e.setAlignment(Label.CENTER);
//Add Label(e) to Frame(a)
a.add(e,BorderLayout.NORTH);

//Add ContainerListener to Panel(c)
c.addContainerListener(cl);
//Set background color(blue) to Panel(c)
c.setBackground(Color.BLUE);
//Add Panel(c) to Frame(a)
a.add(c,BorderLayout.CENTER);

//Panel to place Button(b and d)
Panel f=new Panel();
//Set layout for panel(f)
f.setLayout(new FlowLayout());
//Add ActionListener to Button(b and d)
b.addActionListener(this);
d.addActionListener(this);
//Add button(b and d) to Panel(f)
f.add(b);
f.add(d);
//Add Panel(f) to Frame(a)
a.add(f,BorderLayout.SOUTH);

//Add WindowListener to Frame(a)
a.addWindowListener(wl);
//Set Frame(a) size to 800pixel width and 600pixel height
a.setSize(800,600);
//Set Frame(a) to visible
a.setVisible(true);
}

//Action that must do by Button(b and d)
public void actionPerformed(ActionEvent evt)
{
//Action for Button b
if(evt.getSource()==b)
{
c.add(new Label("NEW LABEL"));
c.validate();
a.validate();
}

//Action for Button d
if(evt.getSource()==d)
{
Component[]storeAllComponent=c.getComponents();
if(storeAllComponent.length!=0)
{
int numbersOfComponent=storeAllComponent.length;
int indexForRemoveComponent=numbersOfComponent-1;
c.remove(indexForRemoveComponent);
c.validate();
a.validate();
}
}
}

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


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

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