package hello; import java.util.GregorianCalendar; import java.util.UUID; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.ui.part.ViewPart; public class TestView extends ViewPart { private Label label; private String jobId; private Display display; public TestView() { System.out.println("TestView.TestView()"); } @Override public void createPartControl(Composite parent) { this.setContentDescription("This is just a test."); GridLayoutFactory.fillDefaults().applyTo(parent); label = new Label(parent, SWT.BOLD | SWT.BORDER); jobId = UUID.randomUUID().toString(); System.out.println("Job ID: " + jobId); display = parent.getDisplay(); Job job = new Job(jobId) { protected IStatus run(IProgressMonitor monitor) { while (!label.isDisposed()) { // Assume this getTime() method takes a long time final String newString = GregorianCalendar.getInstance() .getTime().toString(); System.out.println("new Time: " + newString); display.asyncExec(new Runnable() { public void run() { // Updating UI has to happen in a thread that is called // by display.asyncExec. updateLabel(newString); } }); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); break; } } return Status.OK_STATUS; } }; job.schedule(); } @Override public void setFocus() { System.out.println("TestView.setFocus()"); label.setFocus(); } private void updateLabel(String newString) { System.out.println("TestView.updateLabel()"); if (!label.isDisposed()) { label.setText(newString); } } }