Event handling in java, for the vb.net expert

I found a ton of messages about handling events of existing Java classes, but I did nothing about creating a class with events from scratch.
What is the java translation of this vb.net fragment?

Public Class TestClass Event TestEvent() Sub TestFunction() RaiseEvent TestEvent() End Sub End Class Public Class Form1 Dim WithEvents TC As New TestClass Sub OnTestEvent() Handles TC.TestEvent End Sub End Class 

Thanks.

+4
source share
1 answer

Here's a good reference to the “theory” behind the Java event model:

And here is a link showing how to create your own custom events:

And here is a really good example from fooobar.com/questions/38652 / ... :

 // REFERENCE: /questions/38652/create-a-custom-event-in-java import java.util.*; interface HelloListener { public void someoneSaidHello(); } class Initiater { List<HelloListener> listeners = new ArrayList<HelloListener>(); public void addListener(HelloListener toAdd) { listeners.add(toAdd); } public void sayHello() { System.out.println("Hello!!"); // Notify everybody that may be interested. for (HelloListener hl : listeners) hl.someoneSaidHello(); } } class Responder implements HelloListener { @Override public void someoneSaidHello() { System.out.println("Hello there..."); } } class Test { public static void main(String[] args) { Initiater initiater = new Initiater(); Responder responder = new Responder(); initiater.addListener(responder); initiater.sayHello(); } } 

Key:

1) create an “interface” that defines your “event” (for example, events in the AWT event model).

2) create a class that "implements" this event (similar to "callbacks") in languages ​​such as C, and what VB does automatically for you with the type "Event" is effective.

'Hope this helps ... at least a little!

+3
source

All Articles