Total members 11895 |It is currently Sun Dec 22, 2024 9:56 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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("");
}
}
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Container Event handling with ContainerListener
 What is Event Handling?     -  
 Event Handling Notes     -  
 ComponentListener event handling     -  
 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 Applet, Java AWT






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com