A: 

It doesn't appear that your snippet is complete, but a few things came to mind regarding your problem. You can probably use setEnabled as seen in the snippet below. For more advance things you could look at GridLayout and GridData with the .exclude property in conjunction with setVisible. For reference the SWT Snippets page is really great.

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 App2 {

    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(false);
            }
        });

        button2.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(final SelectionEvent e) {
                button1.setEnabled(false);
                button2.setEnabled(false);
                button3.setEnabled(true);
            }
        });

        button3.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(final SelectionEvent e) {
                button1.setEnabled(true);
                button2.setEnabled(false);
                button3.setEnabled(false);
            }
        });

        shell.open();
        shell.pack();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

    }
}
Hi Jared, Thank you for your response but this doesn't solve my problem. My problem is to enable and disable a button (with a pause in between) within a same function body (widgetSelected method). (i.e. in button1's widgetSelected method, first enabling the button3 and then pausing and then disabling the button3)
Ege
A: 

I am not really sure if you have a problem with the enabling / disabling of the buttons, or putting the delay in between the execution flow.

I have modified Jared's code above to perform both these operations mentioned. Please have a look at this and let me know if this what you were looking for.

Cheers. :-)

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 app1 {

    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 Runnable() {
                    public void run() {

                        try {
                            // Do your operation here.
                            //
                            // Dummy sleep performed here instead.
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                            e1.printStackTrace();
                        }

                    }
                }.run();

                button1.setEnabled(true);
                button2.setEnabled(false);
                button3.setEnabled(false);
            }
        });

        shell.open();
        shell.pack();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

    }
}
Hi Ginu,My aim is to enable some button - do some work - disable the button again. More specifically, I want a cancel button that is enabled only during some process. So when process starts, I want user to be able to cancel button. And when process ends, cancel button should get disabled.Thank you for your response but it didn't work either. What happens is, it does not change the state of button until dummy sleep is done. And when it is done, it does all the updates on the button at the same time. You can see it blinking (it gets disable and enabled consecutively). Thank you though
Ege
+1  A: 

Ok, i've corrected the answer from ginu:

New Runnable().run() does actually not much to nothing, but the idea is correct:

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.

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.

Tested it in Eclipse, looks good so far.

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. :)

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();
        }
    }

}

}

derBiggi
Thanks a lot, it works. However, I still could not get your explanation 100%.I know it is not possible to perform SWT actions in a thread different than the SWT-thread. When I write a SelectionAdapter thatfirst disables all the buttonsthen performs a sleep (in the same thread, without creating a new thread)and then enables all the buttonsit shows indeterministic behavior. Sometimes it disables the 3rd button, sleeps and then enables them all. Sometimes it disables the 2nd button, sleeps and then enables them all, etc. Could you explain me this indeterministic behavior? Thank you again.
Ege