Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok, i've corrected the answer from ginu:</p> <p>New Runnable().run() does actually not much to nothing, but the idea is correct:</p> <p>You need a new thread to do your work in. Problem is, from that thread you can't call setEnabled on the buttons, because that can only be done from within the SWT-Event thread.</p> <p>So you need another runnable to reset the buttons. The second runnable is passed to Display.callAsync and returns before it is actually executed, but that doesn't matter here. You could also use Display.callSync( Runnable ), that call would block your calling thread until the runnable returns. </p> <p>Tested it in Eclipse, looks good so far.</p> <p>Edit: Btw, the reason why calling layout() or Display.update() did not work is that you're currently blocking the SWT-Thread with your work, so the calls to layout/update are queued and only executed when you leave the event handler. Never block an event handler to do long work. :)</p> <pre><code>package test; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class Test { public static void main(final String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new GridLayout(3, false)); final Button button1 = new Button(shell, SWT.PUSH); button1.setText("Click"); final Button button2 = new Button(shell, SWT.PUSH); button2.setText("Me"); final Button button3 = new Button(shell, SWT.PUSH); button3.setText("Dude"); button1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); button2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); button3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); button2.setEnabled(false); button3.setEnabled(false); button1.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(final SelectionEvent e) { button1.setEnabled(false); button2.setEnabled(true); button3.setEnabled(true); new Thread( new Runnable() { public void run() { try { // Do your operation here. // // Dummy sleep performed here instead. Thread.sleep(1000); } catch (InterruptedException e1) { e1.printStackTrace(); } shell.getDisplay().asyncExec( new Runnable() { public void run() { button1.setEnabled(true); button2.setEnabled(false); button3.setEnabled(false); } }); } } ).start(); } }); shell.open(); shell.pack(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload