SWT SWT.WRAP in button

I want to create a checkbox with SWT.WRAP. When I resize the window, the text should be wrapped accordingly.

I created a snippet with two flags:

1) The text does not wrap

2) The text ends, but it does not update when the window is resized

public class GridLayoutSample {
Display display = new Display();
Shell shell = new Shell(display);

private static final String lorem = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ips";

public GridLayoutSample() {

GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);

Button button = new Button(shell, SWT.CHECK | SWT.WRAP);
button.setText(lorem);
button.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL));

RowLayout layout = new RowLayout();
Composite row = new Composite(shell, SWT.NONE);
row.setLayout(layout);
createButton(row, lorem, SWT.CHECK | SWT.WRAP);

shell.pack();
shell.open();

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

display.dispose();
}
public void createButton(Composite parent, String text, int style) {
  RowData layoutData = new RowData();
  layoutData.height = 175;
  layoutData.width = 175;
  Button testButton = new Button(parent, style);
  testButton.setText(text);
  testButton.setLayoutData(layoutData);
}

public static void main(String[] args) {
  new GridLayoutSample();
}
}

Can you help me?

+3
source share
1 answer

Something like this should do this:

Display                     display = new Display();
Shell                       shell   = new Shell(display);

private static final String lorem   = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ips";

public GridLayoutSample()
{
    shell.setLayout(new GridLayout(1, false));
    Composite row = new Composite(shell, SWT.NONE);
    row.setLayout(new GridLayout(1, false));
    row.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    createButton(row, lorem, SWT.CHECK | SWT.WRAP);

    shell.pack();
    shell.open();

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

    display.dispose();
}

public void createButton(final Composite parent, String text, int style)
{
    final GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
    layoutData.widthHint = 175;
    final Button testButton = new Button(parent, style);
    testButton.setText(text);
    testButton.setLayoutData(layoutData);

    parent.getParent().addListener(SWT.Resize, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            layoutData.widthHint = parent.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
            parent.pack();
        }
    });
}

public static void main(String[] args)
{
    new GridLayoutSample();
}

It will resize Buttonbased on the size of the parent.

Here's what it looks like after launch:

enter image description here

And after resizing:

enter image description here

+5
source

All Articles