Java SWT: wrapping syncExec and asyncExec to clean up code
Posted
by jonescb
on Stack Overflow
See other posts from Stack Overflow
or by jonescb
Published on 2010-06-03T19:40:40Z
Indexed on
2010/06/03
19:44 UTC
Read the original article
Hit count: 298
I have a Java Application using SWT as the toolkit, and I'm getting tired of all the ugly boiler plate code it takes to update a GUI element.
Just to set a disabled button to be enabled I have to go through something like this:
shell.getDisplay().asyncExec(new Runnable() {
public void run() {
buttonOk.setEnabled(true);
}
});
I prefer keeping my source code as flat as I possibly can, but I need a whopping 3 indentation levels just to do something simple.
Is there some way I can wrap it? I would like a class like:
public class UIUpdater {
public static void updateUI(Shell shell, *function_ptr*) {
shell.getDisplay().asyncExec(new Runnable() {
public void run() {
//Execute function_ptr
}
});
}
}
And can be used like so:
UIUpdater.updateUI(shell, buttonOk.setEnabled(true));
Something like this would be great for hiding that horrible mess SWT seems to think is necessary to do anything.
As I understand it, Java cannot do functions pointers. But Java 7 will have something called Closures which should be what I want. But in the meantime is there anything at all I can do to pass a function pointer or callback to another function to be executed?
As an aside, I'm starting to think it'd be worth the effort to redo this application in Swing, and I don't have to put up with this ugly crap and non-cross-platformyness of SWT.
© Stack Overflow or respective owner