简体   繁体   中英

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, ie the size it would have if there would be no label. Is there a good practice for achieving this?

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM