Java Event Listener to detect a variable change

前端 未结 7 1712
逝去的感伤
逝去的感伤 2021-02-05 14:38

I cannot seem to find an answer anywhere to my question. Is there any event listener which can detect the changing of a boolean or other variable and then act on it. Or is it p

7条回答
  •  失恋的感觉
    2021-02-05 15:16

    Just like you need to create an event listener, you will also need to create the event firer -- since there is nothing automatic that will do this for you. I've provided sample code that shows you how to implement such a firer.

    This test implementation isn't perfect. It only includes a way to add listeners. You may wish to include a way to remove listeners who are no longer interested in receiving events. Also note that this class is not thread-safe.

    import java.util.List;
    import java.util.ArrayList;
    import java.util.EventListener;
    import java.util.EventObject;
    import java.awt.EventQueue; 
    
    /**
     * 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 BooleanChangeTest implements BooleanChangeDispatcher {
    
        public static void main(String[] args) {
    
            BooleanChangeListener listener = new BooleanChangeListener() {
                @Override
                public void stateChanged(BooleanChangeEvent event) {
                    System.out.println("Detected change to: "
                        + event.getDispatcher().getFlag()
                        + " -- event: " + event);
                }
            };
    
            BooleanChangeTest test = new BooleanChangeTest(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 listeners;
    
        public BooleanChangeTest(boolean initialFlagState) {
            flag = initialFlagState;
            listeners = new ArrayList();
        }
    
        @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;
        }
    }
    

提交回复
热议问题