Note that there are some explanatory texts on larger screens.

plurals
  1. POLayout issue: autogrow label (SWT)
    text
    copied!<p>I'm using a GridLayout trying to make a label autogrow without hiding anything of its content. Here's a simple code to test: Everytime I press the button the label text grows larger, but only after I resize the window horizontally I get the correct layout. Is there any way to fix this without having to resize the window? I think I've tried every property and I still can't get this working, it's driving me nuts!</p> <p><strong>This is what I got</strong></p> <p><img src="https://i.stack.imgur.com/dNyhu.png" alt="wrong layout"></p> <p><strong>This is how it should be</strong></p> <p><img src="https://i.stack.imgur.com/H6rIO.png" alt="right layout"></p> <pre><code>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.Label; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Shell; public class UItest { protected Shell shell; private Label label; public static void main(String[] args) { try { UItest window = new UItest(); window.open(); } catch (Exception e) { e.printStackTrace(); } } public void open() { Display display = Display.getDefault(); createContents(); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } protected void createContents() { shell = new Shell(); shell.setSize(450, 300); shell.setText("SWT Application"); shell.setLayout(new GridLayout(1, false)); Button button = new Button(shell, SWT.NONE); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent arg0) { label.setText(label.getText() + " " + label.getText()); } }); button.setText("New Button"); label = new Label(shell, SWT.WRAP); label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1)); label.setText("New Label"); List list = new List(shell, SWT.BORDER); list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); } protected Label getLabel() { return label; } } </code></pre> <p><strong>Thanks for your time.</strong></p> <hr> <p><strong>SOLVED WITH THESE CHANGES:</strong></p> <pre><code>Button button = new Button(shell, SWT.NONE); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent arg0) { label.setText(label.getText() + " " + label.getText()); shell.layout(); // ADDED THIS } }); button.setText("New Button"); label = new Label(shell, SWT.WRAP); // SET HORIZONTAL GRAB ON LABEL (FIRST TRUE IN GridData CONSTRUCTOR) label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1)); label.setText("New Label"); </code></pre>
 

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