Subscribing to .NET Events

Subscribing to .NET Events

Subscribe to any .NET event with javOnet. Event subscription works the same way as listening Java events. The performance of event subscription is extremely high and allows you to interact with .NET code like it was native Java code. When the event occurs, your Java listener callback is called in a separate thread.

The simplest way to subscribe an event is to use an anonymous Java class.

Example

To create a .NET button and listen for a “Click” event:

	NObject button = Javonet.New("System.Windows.Forms.Button");
	button.set("Text", "Click me!");

	button.addEventListener("Click",new INEventListener() {
		public void eventOccurred(Object[] arguments) {
			System.out.println(".NET event occured");
		}
	});

The anonymous class should implement special INEventListener interface. Alternatively you can write a separate class as the event listener by extending NEventListener and overriding the eventOccurred method, or by implementing the INEventListener interface.

//Your custom Java listener class for .NET events

	public class MyEventListener implements INEventListener
	{
		@Override
		public void eventOccurred(Object[] arguments) {
			System.out.println(".NET Event Occurred");
		}

	}

//Usage of your listener class

	NObject button = Javonet.New("System.Windows.Forms.Button");
	button.set("Text", "Click me!");

	MyEventListener listener = new MyEventListener();
	button.addEventListener("Click",listener);

See Live Example!