title: Detecting a change in an integer
body: <p>Is there any way to detect change in an integer?  Such as creating a listener to listen to the integer to detect and change in value it has.  I know this is possible with booleans with a few tricks but I cannot seem to adapt this to an int value.  Does anyone have any idea how this could be done?  I need to know how to do this in the Java language.  Below is code that I found online that allows for a boolean listener.  How can I convert this to an integer listener?  </p>
  
  <pre><code>import java.util.List;
  import java.util.ArrayList;
  import java.util.EventListener;
  import java.util.EventObject;
  import java.awt.EventQueue; 
  
  //can u see this austin? can u see this i typed this at 9:33 my time
  /**
   * This class uses the EventQueue to process its events, but you should only 
   * really do this if the changes you make have an impact on part of a GUI 
   * eg. adding a button to a JFrame.
   *
   * Otherwise, you should create your own event dispatch thread that can handle
   * change events
   */
  public class test1 implements BooleanChangeDispatcher {
  
      public static void main(String[] args) {
  
          BooleanChangeListener listener = new BooleanChangeListener() { // add this to the class
              @Override
              public void stateChanged(BooleanChangeEvent event) {
                  System.out.println("Detected change to: "
                      + event.getDispatcher().getFlag()
                      + " -- event: " + event);
              }
          };
  
          test1 test = new test1(false);
          test.addBooleanChangeListener(listener);
  
  //        test.setFlag(false); // no change, no event dispatch
  //        test.setFlag(true); // changed to true -- event dispatched
  
      }
  
      private boolean flag;
      private List&lt;BooleanChangeListener&gt; listeners;
  
      public test1(boolean initialFlagState) {
          flag = initialFlagState;
          listeners = new ArrayList&lt;BooleanChangeListener&gt;();
      }
  
      @Override   
      public void addBooleanChangeListener(BooleanChangeListener listener) {
          listeners.add(listener);
      }
  
      @Override
      public void setFlag(boolean flag) {
          if (this.flag != flag) {
              this.flag = flag;
              dispatchEvent();
          }
      }
  
      @Override
      public boolean getFlag() {
          return flag;
      }
  
      private void dispatchEvent() {
          final BooleanChangeEvent event = new BooleanChangeEvent(this);
          for (BooleanChangeListener l : listeners) {
              dispatchRunnableOnEventQueue(l, event);
          }
      }
  
      private void dispatchRunnableOnEventQueue(
                  final BooleanChangeListener listener, 
                  final BooleanChangeEvent event) {
  
          EventQueue.invokeLater(new Runnable() {
              @Override
              public void run() {
                  listener.stateChanged(event);
              }
          });
      }
  
  }
  
  interface BooleanChangeDispatcher {
  
      public void addBooleanChangeListener(BooleanChangeListener listener);
      public boolean getFlag();
      public void setFlag(boolean flag);
  
  }
  
  /**
   * Listener interface for classes interested in knowing about a boolean
   * flag change.
   */
  interface BooleanChangeListener extends EventListener {
  
      public void stateChanged(BooleanChangeEvent event);
  
  }
  
  /** 
   * This class lets the listener know when the change occured and what 
   * object was changed.
   */
  class BooleanChangeEvent extends EventObject {
  
      private final BooleanChangeDispatcher dispatcher;
  
      public BooleanChangeEvent(BooleanChangeDispatcher dispatcher) {
          super(dispatcher);
          this.dispatcher = dispatcher;
      }
  
      // type safe way to get source (as opposed to getSource of EventObject
      public BooleanChangeDispatcher getDispatcher() {
          return dispatcher;
      }
  }
  </code></pre>