OO Programming with Java


Events

Exemple : development of a Timer with a tic event.

  1. Main thread
public class Timer implements Runnable {
  public void start() { new Thread(this).start(); }
  public void stop()  { alive = false; }
  public void run() {
    while ( alive ) {
      try {
        Thread.sleep( delay );
      } catch (InterruptedException e) {
        e.printStackTrace();
      } } }
}

Exercise: define the 'delay' property. Next, add this component to the component to the Window Builder 's palette by using right click. Then, add a timer to an UI ; do you see the delay property ?


6 - 12