This post originated from an RSS feed registered with Java Buzz
by Carlos Villela.
Original Post: SWT gives you the power of the infinite loop!
Feed Title: That's sooo '82!
Feed URL: http://www.jroller.com/rss/cv?catname=Technical
Feed Description: Carlos Villela's weblog. Everyday life, everyday software development, everyday musings.
I was just chatting with Umlauf
tonight when I decided to ask him where he gets his SWT knowledge and
references from. Despite the obvious ones, such as Google, the Eclipse website or the mailing lists and newsgroups, he pointed me to some cool links: one talking about the basic SWT widgets, and the Eclipse Wiki, which I hadn't visited before, and both worth a look if you're into GUI coding.
Browsing these sites a bit, I found one very interesting snippet:
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
Pretty basic Hello World kind of example, but take a look at the while
loop, dispatching all the events - some may argue this is absolutely
awful, but I liked it a lot. If you think about this for a minute,
you'll see that the SWT guys wanted to give us absolute freedom and
visibility about how things are happening: if there needs to be a
dispatcher loop somewhere, it better be on my own code than in the
library, as it happens on Swing. Swing hides so much from you that
debugging it is very hard, sometimes. To debug the event queue on SWT,
all I need is to put a breakpoint inside readAndDispatch, and see
what's happening. If I want to inject some events, to test my
application, for instance, I only need to do it in one place. It's
accessible, and I, just like everyone else, like accessible code, code
that's easy to figure out.
Great. Maybe I even get excited to build GUI apps again :)