Switch to full style
Graphics and animation Java code examples
Post a reply

Container Event handling with ContainerListener

Sat Feb 09, 2013 3:56 pm

Container Event handling with ContainerListener
java code
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class ContainerEventDemo extends Applet
implements ContainerListener,
ActionListener {
TextArea textAreaObj;
Panel panelForBtns;
Button btnAdd, btnRemove, btnClear;
Vector vectorOfBtns;
static final String ADD = "add";
static final String REMOVE = "remove";
static final String CLEAR = "clear";
String lineSeparator;

public void init() {
lineSeparator = System.getProperty("line.separator");

//Initialize an empty list of buttons.
vectorOfBtns = new Vector(10, 10);

//Create all the components.
btnAdd = new Button("Add a button");
btnAdd.setActionCommand(ADD);
btnAdd.addActionListener(this);

btnRemove = new Button("Remove a button");
btnRemove.setActionCommand(REMOVE);
btnRemove.addActionListener(this);

panelForBtns = new Panel();
panelForBtns.addContainerListener(this);

textAreaObj = new TextArea(5, 20);
textAreaObj.setEditable(false);

btnClear = new Button("Clear text area");
btnClear.setActionCommand(CLEAR);
btnClear.addActionListener(this);

//Lay out the components.
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gridBagConstraint = new GridBagConstraints();
setLayout(gridbag);
gridBagConstraint.fill = GridBagConstraints.BOTH; //Fill entire cell.

gridBagConstraint.weighty = 1.0; //Button area and message area have equal height.
gridBagConstraint.gridwidth = GridBagConstraints.REMAINDER; //end of row
gridbag.setConstraints(textAreaObj, gridBagConstraint);
add(textAreaObj);

gridBagConstraint.weighty = 0.0;
gridbag.setConstraints(btnClear, gridBagConstraint);
add(btnClear);

gridBagConstraint.weightx = 1.0; //Add/remove buttons have equal width.
gridBagConstraint.gridwidth = 1; //NOT end of row
gridbag.setConstraints(btnAdd, gridBagConstraint);
add(btnAdd);

gridBagConstraint.gridwidth = GridBagConstraints.REMAINDER; //end of row
gridbag.setConstraints(btnRemove, gridBagConstraint);
add(btnRemove);

gridBagConstraint.weighty = 1.0; //Button area and message area have equal height.
gridbag.setConstraints(panelForBtns, gridBagConstraint);
add(panelForBtns);
}

public void componentAdded(ContainerEvent e) {
displayMessage(" added to ", e);
}

public void componentRemoved(ContainerEvent e) {
displayMessage(" removed from ", e);
}

void displayMessage(String action, ContainerEvent e) {
textAreaObj.append(((Button)e.getChild()).getLabel()
+ " was"
+ action
+ e.getContainer().getClass().getName()
+ lineSeparator);
}


public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();

if (command == ADD) {
Button newButton = new Button("Button: "
+ (vectorOfBtns.size() + 1));
vectorOfBtns.addElement(newButton);
panelForBtns.add(newButton);
panelForBtns.validate();
} else if (command == REMOVE) {
int lastIndex = vectorOfBtns.size() - 1;
try {
Button nixedButton = (Button)vectorOfBtns.elementAt(lastIndex);
panelForBtns.remove(nixedButton);
vectorOfBtns.removeElementAt(lastIndex);
panelForBtns.validate();
} catch (ArrayIndexOutOfBoundsException exc) {
}
} else if (command == CLEAR) {
textAreaObj.setText("");
}
}
}




Post a reply
  Related Posts  to : Container Event handling with ContainerListener
 What is Event Handling?     -  
 ComponentListener event handling     -  
 Event Handling Notes     -  
 explain event handling in java     -  
 java.util classes and interfaces support event handling     -  
 advantage of the event delegation model over event-inherit     -  
 relationship an event-listener interface & event handler     -  
 specify a container's layout     -  
 Portlet container question     -  
 AWT event hierarchy     -  

Topic Tags

Java AWT, Java Applet