Comments on: WireLoad releases SwtCallback Java Library The Internet Startup Blog Sat, 24 Oct 2009 19:07:30 +0000 hourly 1 By: Playing With Wire » Java library SwtCallback 0.2 released, helps avoid Runnable interface Sun, 10 Jun 2007 22:59:36 +0000 […] I wrote in my release post about SwtCallback, SwtCallback is a java library which adds an alternative way to handle events in […]

By: Alexander Ljungberg Mon, 04 Jun 2007 17:11:08 +0000 Like you touch upon in your comment there are definitely a couple of downsides. I assume that by the type safety problem you refer to the fact that there is no compile time checking of your event handling methods. This is true, but this problem is alleviated by the fact that callbacks are usually registered early on during program execution, and that SwtCallback will throw an exception at this time if it can’t find an appropriate handler. This means that for most programs you’ll know immediately that there’s a problem.

A surprising side effect of this runtime check is that it avoids a problem with using Adapters: if you subclass an adapter and you try to override one of its methods, you may actually typo the override method and neither the compiler nor the runtime environment would be able to detect this. With SwtCallback a typo like that would be detected at runtime. (The @overrride tag is another solution to this problem.)

As for refactoring, you’re right. Eclipse wouldn’t be able to rename one of these methods. In practice the callbacks are usually declared in the very same class or possibly in the corresponding view class for a particular controller. In the rare case that you do want to rename a method you would indeed have to suffer changing two different places by hand, but at least the other place you need to change is usually close at hand.

You don’t need to lose the event object. If your callback method signature includes an event parameter, SwtCallback will pass the method the corresponding event object when calling it.

Using an anonymous inner class to call out to a private handler is a good solution which I like too. I still prefer the one line solution of SwtCallback as it’s shorter and less complex: I don’t have to remember that the method I need to override is ‘public void widgetSelected(…)’ in a SelectionAdapter. With an Swtcallback, all I have to do is to select the right SWT.XXX identifier and I’m good to go. Every handler I make will have this consistent syntax.

Thank you for your interesting comments. To summarize: yes, there is no compile time checking of the provided method. On the flip side there’s solid runtime checking. You don’t need to lose the Event object when using SwtCallback. Finally, SwtCallback is easier on the programmer’s memory and less complex than anonymous extended adapters in inner classes, at least in my taste.

That said, Mr. Pryor’s presented solution is a good alternative to SwtCallback, especially if you wish to be able to refactor your handler methods, and you desire a solution that is statically correct instead of being a dynamic runtime solution.

By: Ben Pryor Mon, 04 Jun 2007 16:10:05 +0000 This is an interesting approach. I’d be interested to hear your thoughts on one of the big downsides to this method, which is that you give up type safety and the refactoring wizard won’t change the string literal when you refactor the method name. To me (others may disagree) this is a lot to give up for a little extra typing.

I didn’t look at the code behind this at all, but don’t you also lose the event object with this approach? You don’t always need it but sometimes you do.

Also, the recommended SWT approach is to have anon. inner class methods delegate to an identically named private method in the outer class:

myButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {

private void widgetSelected(SelectionEvent e) {


You could of course change the private outer class method to something that was named more specifically if you had more than one of these handlers.

To my eyes anyway, the above method is as readable as using the callback library and has the advantage of type safety and refactorability.

Anyways, like I said an interesting approach – I’d enjoy hearing your comments on the above.