import java.awt.*; import java.awt.event.*; public class NestedPanels2 extends NestedPanels implements ActionListener, ItemListener { public NestedPanels2() { // set up all of the buttons and what not super(); // add the action listeners to the widgets choice.addItemListener(this); beActionListener(this); } // do this when the select box changes value public void itemStateChanged(ItemEvent event) { if (event!=null) System.out.println("ItemStateChanged: " + event); Choice choice = (Choice) event.getSource(); if (choice != null) messageBar.setText("Choice selected: " + event.getItem()); } // do this when a button is pressed public void actionPerformed(ActionEvent event) { if (event!=null) System.out.println("ActionPerformed: " + event); Button source = (Button) event.getSource(); if (source != null) messageBar.setText("Button pushed: " + source.getLabel()); } // a recursive function that assigns action listeners // to all of the buttons. // uses the fact that a container can contain buttons. protected void beActionListener(Component comp) { if (comp != null) { if (comp instanceof Button) { // do this if you've got a button Button button = (Button) comp; button.addActionListener(this); } else if (comp instanceof Container) { // get all of the sub components of the container // and call this recursively. Container container = (Container) comp; int n = container.getComponentCount(); for (int i = 0; i < n; i++) beActionListener(container.getComponent(i)); } } } }