Sat Feb 09, 2013 3:56 pm
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("");
}
}
}
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
Powered by phpBB © phpBB Group.