0

I'm using an event system based on the suggestions in this question: Create Custom Event in Java

I implemented it in a component to handle and passthrough the events from a button on the component. I put the component into it's own jar file. Then I used the component and jar file in another project and when I tried to run the program I had created with it, it gave me the following error:

java.lang.IllegalAccessException: Class Eventing.EventHandler can not access a member of class outfit.proto.frmDo$1 with modifiers "public"

This is the code of the EventHandler class I wrote:

public class EventHandler<T> {
    private ArrayList<T> listenerPool = new ArrayList<>();

    public void addListener(T listener){
        listenerPool.add(listener);
    }

    public void raiseEvent(Object eventData){
        for (T listener : listenerPool){
                    try {
                        if (eventData != null) {
                            listener.getClass().getDeclaredMethods()[0].invoke(listener, eventData);
                        } else {
                            listener.getClass().getDeclaredMethods()[0].invoke(listener);
                        }
                    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                        Logger.getLogger(EventHandler.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }

This is how I setup the the EventHandler class on the component:

EventHandler<EventListener> _loginEvent = new EventHandler<>();

public EventHandler<EventListener> loginEvent(){
    return _loginEvent;
}

loginButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            _loginEvent.raiseEvent(null);
        }
});

This is what I did to catch the events on frmDo:

   this.component2.loginEvent().addListener(new EventAdapter() {
       @Override
       public void executeResult() {

       }
   });
Community
  • 1
  • 1
Feanaro
  • 922
  • 3
  • 19
  • 35

1 Answers1

0

Based on this wikipedia entry on Observer Patterns I got the event system to work! As I suspected Java has some pre-defined system for these kind of things.

Feanaro
  • 922
  • 3
  • 19
  • 35