Note that there are some explanatory texts on larger screens.

plurals
  1. POSWT update / redraw / layout prοblem
    text
    copied!<p>I know many people experience this problem, but the solutions I found online do not seem to solve mine. I have a composite that has three buttons. What I want is the following : When I click one button, I want some other button to be grayed out ( setEnabled(false) ) and after a while (after a method execution), I want the button to be enabled again.</p> <p>Many such problems are solved by calling layout() method on the parent container, or <a href="https://stackoverflow.com/questions/741929/how-to-force-redraw-of-an-ex-invisible-groups-content-in-swt">this very similar one</a> is solved by calling Display.getCurrent().update();</p> <p>Simply, my code could be summarized as follows : </p> <pre><code> import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Button; public class app1 { protected Shell shell; /** * Launch the application. * @param args */ public static void main(String[] args) { try { app1 window = new app1(); window.open(); } catch (Exception e) { e.printStackTrace(); } } /** * Open the window. */ public void open() { Display display = Display.getDefault(); createContents(); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } /** * Create contents of the window. */ Button button1 , button2 , button3; Label label; protected void createContents() { shell = new Shell(); shell.setSize(450, 300); shell.setText("SWT Application"); shell.setLayout(new GridLayout(1,false)); { final Composite composite = new Composite(shell, SWT.NONE); composite.setLayout(new GridLayout(3,false)); GridData gd_composite = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL); gd_composite.grabExcessHorizontalSpace = true; gd_composite.horizontalSpan = 10; //? gd_composite.verticalIndent = 5; composite.setLayoutData(gd_composite); GridData gd_button; { button1 = new Button(composite, SWT.NONE); button1.setText("Button 1"); gd_button = new GridData(SWT.FILL, GridData.BEGINNING, false, false); gd_button.horizontalSpan = 1; button1.setLayoutData(gd_button); button1.addSelectionListener(new SelectionListener(){ public void widgetSelected(SelectionEvent e){ try{ button2.setEnabled(false); button2.redraw(); button2.update(); //composite.redraw(); //composite.update(); //composite.layout(); shell.redraw(); shell.update(); shell.layout(); Display.getCurrent().update(); } catch (Exception e2) { System.err.println("exception e : " + e2.toString()); } System.out.println("basla"); try { System.out.println("sleep1"); Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(); } catch (Throwable th) { System.err.println("th: " + th.toString()); } try { System.out.println("sleep2"); Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(); } catch (Throwable th) { System.err.println("th: " + th.toString()); } try { System.out.println("sleep3"); Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(); } catch (Throwable th) { System.err.println("th: " + th.toString()); } for(int i=0 ; i &lt 10000 ; i++) { System.out.println(i); } } public void widgetDefaultSelected(SelectionEvent e) { System.err.println("widgetDefault !"); } }); } { button2 = new Button(composite, SWT.NONE); button2.setText("Button 2"); gd_button = new GridData(SWT.FILL, GridData.CENTER, false, false); gd_button.horizontalSpan = 1; button2.setLayoutData(gd_button); button2.addSelectionListener(new SelectionListener(){ public void widgetSelected(SelectionEvent e){ button1.setEnabled(false); composite.layout(); for (int i=1; i&lt=100; i++) { try { Thread.sleep(10); } catch (Throwable th) {} label.setText(i + " %"); label.update(); } } public void widgetDefaultSelected(SelectionEvent e) {} }); } { label = new Label(composite , SWT.NONE); label.setText("0 %"); label.update(); } } } } </code></pre> <p>What happens is, the button gets disabled after the end of widgetSelected() method is reached. However, the label gets updated frequently without any problem (even when the label.update() method is not there)</p> <p>Additional information : Say, I disable the button, then put a Thread.sleep() and then enable the button ; it sleeps first and then quickly disables and enables the button. So I believe all such paint requests are queued and are processed at the end of the execution.</p> <p><b>Useful information:</b> I realized that, when I create and display a MessageBox right after my display changes, the display changes occur. So, if I make the following change in my widgetSelected method :</p> <pre><code> button2.setEnabled(false) MessageBox mBox = new MessageBox(Display.getCurrent().getActiveShell(), SWT.ICON_INFORMATION | SWT.OK); mBox.setText("Information"); mBox.setMessage("Buttons updated!"); mBox.open(); </code></pre> <p>the button will be grayed out as soon as the widgetSelected() method is called. This makes me believe my solution lies within Display.getCurrent() methods. However, I tried </p> <pre> Display.getCurrent().getActiveShell().redraw() Display.getCurrent().getActiveShell().update() Display.getCurrent().getActiveShell().layout() </pre> <p>methods and they didnt solve my problem.</p> <p>Thanks, Ege</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