public class NestedPanels3
class ChoiceEventHandler implements ItemListener
class ButtonEventHandler implements ActionListener
1: import java.awt.*;
2: import java.awt.event.*;
3:
4: public class NestedPanels3
5: extends NestedPanels {
6: public NestedPanels3() {
7: super();
8: ChoiceEventHandler chandler = new ChoiceEventHandler();
9: choice.addItemListener(chandler);
10: ButtonEventHandler bhandler = new ButtonEventHandler();
11: bhandler.beActionListener(this);
12:
13: }
14:
15: class ChoiceEventHandler implements ItemListener {
16: public void itemStateChanged(ItemEvent event) {
17: if (event!=null)
18: System.out.println("ItemStateChanged: " + event);
19: Choice choice = (Choice) event.getSource();
20: if (choice != null)
21: messageBar.setText("Choice selected: " + event.getItem());
22: }
23: }
24: class ButtonEventHandler implements ActionListener {
25: public void actionPerformed(ActionEvent event) {
26: if (event!=null)
27: System.out.println("ActionPerformed: " + event);
28: Button source = (Button) event.getSource();
29: if (source != null)
30: messageBar.setText("Button pushed: " + source.getLabel());
31: }
32:
33: protected void beActionListener(Component comp) {
34: if (comp != null) {
35: if (comp instanceof Button) {
36: Button button = (Button) comp;
37: button.addActionListener(this);
38: } else if (comp instanceof Container) {
39: Container container = (Container) comp;
40: int n = container.getComponentCount();
41: for (int i = 0; i < n; i++)
42: beActionListener(container.getComponent(i));
43: }
44: }
45: }
46: }
47:
48: }