SWT tip: Using SelectionAdapter

This one is probably for newbies.

When providing a SelectionListener for doing a something when a SWT button is pressed, you’ll need to implement two methods:


Button b = new Button(container, SWT.PUSH);
b.addSelectionListener( new SelectionListener()
{
  public void widgetSelected( SelectionEvent e )
  {
    // TODO Auto-generated method stub
  }

  public void widgetDefaultSelected( SelectionEvent e )
  {
    // TODO Auto-generated method stub
  }
} );

However, only one of those methods will be called: widgetSelected

So you can save a couple of lines (which is always nice, specially when inlining the handlers like in these examples) by using SelectionAdapter, an implementation of SelectionListener with default implementation of both methods (which is to do nothing).

The final, shorter, core looks like this:


Button b = new Button(container, SWT.PUSH);
b.addSelectionListener( new SelectionAdapter()
{
  public void widgetSelected( SelectionEvent e )
  {
    doSomething();
  }
} );

I’m on Twitter now

I finally gave up on trying to resist. Curiosity was stronger… so now you can read my tweets at http://twitter.com/amuino

Q4E Screencasts available on mirrors

I’ve received notice from Jing (owners of screencast.com where q4e screencasts are hosted) that the allowed bandwidth is about to be exceeded… Thanks for the interest!

As a reminder, you can watch the WTP Screencast from Joakim’s site.