Wrapping text in Eclipse Wizard

An Eclipse Wizard computes the size of its window depending on the size of the largest page of the wizard. That’s why I experience difficulties making a Label to wrap it’s text in a WizardPage. I use the following code snippet to create a wrapped label:

@Override
public void createControl(Composite parent)
{
    initializeDialogUnits(parent);
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setFont(parent.getFont());
    composite.setLayout(initGridLayout(new GridLayout(3, false), true));
    composite.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

    Label l = new Label(composite, SWT.WRAP);
    l.setText("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.");
    l.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false, 3, 1));

    // create other components

    setControl(composite);
}

As the wizard shows up, it tries to put the whole text in the single line and its window takes as much place as it needs. I think you can imagine how it looks. I get the desired result if I manually resize window to its original size, i.e. the size it would have if there would be no label. Is there a good practice for achieving this?

enter image description here

Answer

You can set the widthHint attribute on the GridData instance. It’s not a perfect solution, but maybe it helps:

Label l = new Label(composite, SWT.WRAP);
l.setText("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.");
GridData gd = new GridData(SWT.FILL, SWT.NONE, true, false, 3, 1);
l.setLayoutData(gd);

If you won’t set the widthHint attribute to a static value, the only way I figured out is to override the setVisible method of the DialogPage. If you do so, you need to keep the label instance as member variable in the class.

@Override
public void setVisible(boolean visible) {
    int widthHint = getControl().getSize().x - 16;

    l.setText("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.");
    ((GridData)l.getLayoutData()).widthHint = widthHint;

    getControl().pack();

    super.setVisible(visible);
}

Hope that helps.

Leave a Reply

Your email address will not be published. Required fields are marked *