//: c13:DynamicEvents.java
// Przykład z książki 'Thinking in Java, wydanie 2.', autor - Bruce Eckel
// www.BruceEckel.com
// Patrz informacje o prawach autorskich podane w pliku CopyRight.txt.
// Obsługa zdarzeń może być zmieniana dynamicznie.
// Program pokazuje również jednoczesne
// wykonanie wielu akcji na jedno zdarzenie.
// <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("Przycisk1"),
b2 = new JButton("Przycisk2");
JTextArea txt = new JTextArea();
class B implements ActionListener {
public void actionPerformed(ActionEvent e) {
txt.append("Naciśnięto przycisk\n");
}
}
class CountListener implements ActionListener {
int index;
public CountListener(int i) { index = i; }
public void actionPerformed(ActionEvent e) {
txt.append("Odbiorca numer "+index+"\n");
}
}
class B1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
txt.append("Naciśnięto przycisk 1\n");
ActionListener a = new CountListener(i++);
v.add(a);
b2.addActionListener(a);
}
}
class B2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
txt.append("Naciśnięto przycisk 2\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);
}
} ///:~