EZ Jcom
Java to COM/ActiveX Bridge

Receiving events from Java

This page covers details of receiving Java events from C# and Visual Basic.

Java Interfaces

Java events are received on Java Interfaces. In order to receive events, you must add the necessary interfaces to the list of Java classes you wish to access. If you imported an entire Jar file, any interfaces within it should have automatically been added for you.

Declaring and instantiating the interface

To receive events, an "Event Sink" class for the interface is created, by attaching the string _EventSink to the class name. For instance, if the interface is named "MyEvent", EZ JCom will also create an events sink class corresponding to it, named "MyEvent_EventSink".

You would need to

  • Declare and instantiate an object of the event sink class. interface. In C#, this is just done by a "new", e.g.
        MyLib.MyEvent_EventSink myEventSink = new MyLib.MyEvent_EventSink();
    
    In Visual Basic, WithEvents is used to declare the event, e.g.
        Public WithEvents myEventSink as MyLib.MyEvent_EventSink
    
  • In Visual Basic, the Event Sink needs to be instantiated in code, as
        Set myEventSink = new MyLib.MyEvent_EventSink
    
    or in VB.NET as
        myEventSink = new MyLib.MyEvent_EventSink()
    
  • Instantiate the Java object for the interface, and connect it to the event sink e.g.
        MyLib.MyEvent myEvent = new MyLib.MyEvent();
        myEvent.ConstructFromEventSink( myEventSink );
    
  • Pass the myEvent Java object to any event generator methods that are expecting an interface of that type.

Adding event handlers

In C# IDE's, if you type the name of the event sink object and enter a period (dot) character, Intellisense will show you the available interfaces. If you select an event name and then enter the += sequence, Intellisense will offer to complete the statement for you. Press TAB, and the code to add an event handler will be added. Press TAB again, and an event handler method will also be generated. You can also do this manually.

In Visual Basic (VB.NET and earlier versions), in the "Code" view, the event sink object will be available for selection on the left side. When the event object has been selected on the left, on the right side you can select one of the handlers to implement, and the IDE will add the event handler framework.

In VB.NET, instead of this approach you can also use AddHandler to add specific handlers, e.g.

    AddHandler myEventSink.OnSomeEvent, AddressOf MyEventhandler