OO Programming with Java


Events

Components generate Event(s): mouse click, keyboard pressed, etc. that can be used by the way of Listerner(s). This corresponds to an Observer design pattern.

An illustration is:

btn1.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    clicked(e);
  } });

public void clicked(ActionEvent e) {
 String btn = ((JButton)e.getSource()).getText();
}

NB. In WindowBuilder, right click on a component to get events' list.

Exercice : what is the UML model for the preceding code ?


5 - 12