Pages

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
*****************************************************************
Related Posts Plugin for WordPress, Blogger...