Event Handling in Java

How to raise user events and process in Java. Some links will be helpful.

Thanks.

+6
java events
source share
6 answers
+1
source share

There are no first-class events in Java. All event processing is performed using interfaces and a listener template. For example:

// Roughly analogous to .NET EventArgs class ClickEvent extends EventObject { public ClickEvent(Object source) { super(source); } } // Roughly analogous to .NET delegate interface ClickListener extends EventListener { void clicked(ClickEvent e); } class Button { // Two methods and field roughly analogous to .NET event with explicit add and remove accessors // Listener list is typically reused between several events private EventListenerList listenerList = new EventListenerList(); void addClickListener(ClickListener l) { clickListenerList.add(ClickListener.class, l) } void removeClickListener(ClickListener l) { clickListenerList.remove(ClickListener.class, l) } // Roughly analogous to .net OnEvent protected virtual method pattern - // call this method to raise the event protected void fireClicked(ClickEvent e) { ClickListener[] ls = listenerList.getListeners(ClickEvent.class); for (ClickListener l : ls) { l.clicked(e); } } } 

Client code typically uses anonymous inner classes to register handlers:

 Button b = new Button(); b.addClickListener(new ClickListener() { public void clicked(ClickEvent e) { // handle event } }); 
+15
source share

There is no internal event handling in Java, but there are libraries to help you do this. Check out javaEventing, http://code.google.com/p/javaeventing/ It works the same way as in C #, where you first define your events and then register event listeners. You fire events using EventManager.triggerEvent (.. someEvent). It also allows you to create custom conditions and payload.

bean

+5
source share

There is no built-in support for delegates and events in Java, such as C #. You will need to implement Observer or Publish / Sign .

+3
source share

If you are looking for a .net type delegate event, I suggest this templated solution. The advantage is that no hard clicks are required, and the listener can implement several โ€œeventsโ€ if they use different classes of events.

 import java.util.EventListener; import java.util.EventObject; // replaces the .net delegate public interface GenericEventListener<EventArgsType extends EventObject> extends EventListener { public void eventFired(EventArgsType e); } //------------------------------------------------ import java.util.EventObject; import java.util.Vector; // replaces the .net Event keyword public class GenericEventSource<EventArgsType extends EventObject> { private Vector<GenericEventListener<EventArgsType>> listenerList = new Vector<GenericEventListener<EventArgsType>>(); //TODO handle multi-threading lock issues public void addListener(GenericEventListener<EventArgsType> listener) { listenerList.add(listener); } //TODO handle multi-threading lock issues public void raise(EventArgsType e) { for (GenericEventListener<EventArgsType> listener : listenerList) { listener.eventFired(e); } } } //------------------------------------------------ // like a .net class extending EventArgs public class MyCustomEventArgs extends EventObject { private int arg; public MyCustomEventArgs(Object source, int arg) { super(source); this.arg = arg; } public int getArg() { return arg; } } //------------------------------------------------ // replaces the .net event handler function. Can be put in a nested class, Java style // Listener can handle several event types if they have different EventArg classes public class MyCustomListener implements GenericEventListener<MyCustomEventArgs> { private Object source = null; private int arg; public void eventFired(MyCustomEventArgs e) { source = e.getSource(); arg = e.getArg(); // continue handling event... } } //------------------------------------------------ import GenericEventListener; import GenericEventSource; // this is the class that would like to throw events (eg MyButton) public class MyCustomEventSource { // This is like declaring a .net public Event field of a specific delegate type GenericEventSource<MyCustomEventArgs> myEvent = new GenericEventSource<MyCustomEventArgs>(); public GenericEventSource<MyCustomEventArgs> getEvent() { return myEvent; } } //------------------------------------------------ // Examples of using the event MyCustomListener myListener1 = new MyCustomListener(); MyCustomEventSource mySource = new MyCustomEventSource(); mySource.getEvent().addListener( myListener1 ); mySource.getEvent().raise( new MyCustomEventArgs(mySource,5)); 
+2
source share

There is no language support in Java for event handling. But there are some classes that can help. You can look at the java.awt.Event class; java.awt.Event and java.beans packages. The first package is the base for event handling in AWT and Swing GUI libraries. java.beans contains supporting materials for the Java Beans specification , including property change events and bean context events.

Events are typically handled according to the Observer or Publish / Subscribe patterns (as indicated by kgiannakakis )

0
source share

All Articles