Do you know the SWT templates in Eclipse?

Do you know the SWT templates in Eclipse?

Often when I use SWT templates in a presentation, some people look amazed, so I thought it’s worth sharing. If you write SWT code and don’t know these templates yet, you may be wasting time typing the same code over and over again.

Whenever you write an SWT snippet, you start with a Display, a Shell, and the SWT mainloop. That’s this piece of code that you can never remember, something with readAndDispatch() and display.sleep(). But Eclipse can help you out. You only need to type main and hit Ctrl+Space.

mainloop template

Select SWT mainloop (pro tip: you can use the ↓ key for that 😉) and there you go:

SWT mainloop code

Well, and if you want to create a push button next, there’s also a template for that:

SWT button template

button template code

You even get a selection listener for free, as you’re probably going to need it.

There are templates for most SWT widgets, for GridLayout and GridData, and also for running code in UI thread (try execute). Of course, you can edit the templates to fit your needs (Preferences > Editor > Templates) and if you’re missing something, you can add your own templates.