Skip to content

Instantly share code, notes, and snippets.

@Dollyn
Created August 14, 2014 14:09
Show Gist options
  • Save Dollyn/67fbf387869c5b5080d6 to your computer and use it in GitHub Desktop.
Save Dollyn/67fbf387869c5b5080d6 to your computer and use it in GitHub Desktop.
Swt non-ui thread example. A clock app.
package test;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class Clock {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Label label = new Label(shell, SWT.NONE);
label.setText(new SimpleDateFormat("HH:mm:ss").format(new Date()));
new Thread() {
public void run() {
while(true) {
try {
Thread.sleep(1000);
display.asyncExec(new Runnable() {
@Override
public void run() {
label.setText(new SimpleDateFormat("HH:mm:ss").format(new Date()));
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
shell.setLayout(new RowLayout());
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment