Is it possible to write your own objects that give

2019-01-29 03:17发布

问题:

I've looked at the java tutorials online and they all seem concerned with catching ActionEvents given out by other components that are already written. Is it possible to write your own objects that have there own set of criteria that trigger actionEvents that can then be caught by other classes that have registered as listeners?

So for example: If I wanted an object that was counting sheep to send out an actionEvent when 100 sheep had been counted to all the sleeper objects that had registered as listeners.

Is there a way to do this are there any tutorials online?

Any help is greatly appreciated.

回答1:

Yes, it's pretty straightforward, once someone shows you how to create your own listeners.

First, you create your own EventObject. Here's an example from one of my projects.

import gov.bop.rabid.datahandler.bean.InmateDataBean;

import java.util.EventObject;

public class InmatePhotoEventObject extends EventObject {

    private static final long serialVersionUID = 1L;

    protected InmateDataBean inmate;

    public InmatePhotoEventObject(Object source) {
        super(source);
    }

    public InmateDataBean getInmate() {
        return inmate;
    }

    public void setInmate(InmateDataBean inmate) {
        this.inmate = inmate;
    }

}

There's nothing special about this class, other than it extends EventObject. Your constructor is defined by EventObject, but you can create any methods you want.

Second, you define an EventListener interface.

public interface EventListener {

    public void handleEvent(InmatePhotoEventObject eo);

}

You would use the EventObject you created. You can use any method name or names that you want. This is the interface for the code that will be written as a response to the listener.

Third, you write a ListenerHandler. Here's mine from the same project.

import gov.bop.rabid.datahandler.bean.InmateDataBean;
import gov.bop.rabid.datahandler.main.EventListener;
import gov.bop.rabid.datahandler.main.InmatePhotoEventListener;
import gov.bop.rabid.datahandler.main.InmatePhotoEventObject;

import java.util.ArrayList;
import java.util.List;

public class InmatePhotoListenerHandler {

    protected List<EventListener> listeners;

    public InmatePhotoListenerHandler() {
        listeners = new ArrayList<EventListener>();
    }

    public void addListener(EventListener listener) {
        listeners.add(listener);
    }

    public void removeListener(EventListener listener) {
        for (int i = listeners.size() - 1; i >= 0; i--) {
            EventListener instance = listeners.get(i);
            if (instance.equals(listener)) {
                listeners.remove(i);
            }
        }
    }

    public void fireEvent(final InmatePhotoEventObject eo, 
            final InmateDataBean inmate) {
        for (int i = 0; i < listeners.size(); i++) {
            final EventListener instance = listeners.get(i);
            Runnable runnable = new Runnable() {
                public void run() {
                    eo.setInmate(inmate);
                    instance.handleEvent(eo);
                }

            };
            new Thread(runnable).start();
        }
    }

    public static void main(String[] args) {
        System.out.println("This line goes in your DataHandlerMain class "
                + "constructor.");
        InmatePhotoListenerHandler handler = new InmatePhotoListenerHandler();
        System.out.println("I need you to put the commented method in "
                + "DataHandlerMain so I can use the handler instance.");

        // public InmatePhotoListenerHandler getInmatePhotoListenerHandler() {
        //      return handler;
        // }

        System.out.println("This line goes in the GUI code.");
        handler.addListener(new InmatePhotoEventListener());

        System.out.println("Later, when you've received the response from "
                + "the web service...");
        InmateDataBean inmate = new InmateDataBean();
        inmate.setIntKey(23);
        handler.fireEvent(new InmatePhotoEventObject(handler), inmate);
    }
}

The main method in this class shows you how you use a ListenerHandler. The rest of the methods in the class are standard. You would use your own EventObject and EventListener.



回答2:

Yes.

I suggest you look at the java API documentation for ActionEvent and EventListenerList.

I also suggest that you read about the Listener (also called Observer) pattern.