//: c13:DynamicEvents.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// You can change event behavior dynamically.
// Also shows multiple actions for an event.
// <applet code=DynamicEvents
// width=250 height=400></applet>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.bruceeckel.swing.*;
public class DynamicEvents extends JApplet {
ArrayList v = new ArrayList();
int i = 0;
JButton
b1 = new JButton("Button1"),
b2 = new JButton("Button2");
JTextArea txt = new JTextArea();
class B implements ActionListener {
public void actionPerformed(ActionEvent e) {
txt.append("A button was pressed\n");
}
}
class CountListener implements ActionListener {
int index;
public CountListener(int i) { index = i; }
public void actionPerformed(ActionEvent e) {
txt.append("Counted Listener "+index+"\n");
}
}
class B1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
txt.append("Button 1 pressed\n");
ActionListener a = new CountListener(i++);
v.add(a);
b2.addActionListener(a);
}
}
class B2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
txt.append("Button2 pressed\n");
int end = v.size() - 1;
if(end >= 0) {
b2.removeActionListener(
(ActionListener)v.get(end));
v.remove(end);
}
}
}
public void init() {
Container cp = getContentPane();
b1.addActionListener(new B());
b1.addActionListener(new B1());
b2.addActionListener(new B());
b2.addActionListener(new B2());
JPanel p = new JPanel();
p.add(b1);
p.add(b2);
cp.add(BorderLayout.NORTH, p);
cp.add(new JScrollPane(txt));
}
public static void main(String[] args) {
Console.run(new DynamicEvents(), 250, 400);
}
} ///:~