Akki
Akki

Reputation: 1

SWT Label inside a scrolled composite is not wrapping dynamically

I am using a scrolled composite which contains a composite inside it and that composite contains a label. This whole scrolled composite is inside a jface dialog.

My problem is that without hardcoding the width I need to set the size of inner composite such that it will wrap the whole label and will display inside the scrolled composite.

Below is the code which I have:

     protected Control createDialogArea(Composite parent)
        {
        final ScrolledComposite scrolledComposite = new ScrolledComposite(parent,
                        SWT.H_SCROLL | SWT.V_SCROLL);
                GridLayoutFactory.fillDefaults().applyTo(scrolledComposite);
                GridDataFactory.fillDefaults().grab(true, true).hint(500, 400).applyTo(scrolledComposite);
                scrolledComposite.setExpandHorizontal(true);
                scrolledComposite.setExpandVertical(true);
                scrolledComposite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));

                final Composite innerComposite = new Composite(scrolledComposite, SWT.NONE);
                GridLayoutFactory.fillDefaults().applyTo(innerComposite);
                GridDataFactory.fillDefaults().grab(true, true).applyTo(innerComposite);
                scrolledComposite.setContent(innerComposite);
                innerComposite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_CYAN));

                messageLabel = new Label(innerComposite, SWT.WRAP);
                messageLabel.setText("Some Long message");
                GridDataFactory.fillDefaults().grab(true, true).applyTo(messageLabel);
                scrolledComposite.setMinSize(innerComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
                innerComposite.layout();
}

If I use SWT.Default in the setMinSize, my label won't get wrapped. If I use parent.getClientArea().width in setMinSize my label will get wrapped but with that I end up with an infinite vertical scroll.

The only time it works is when I hard-code the width, which I don't want to do. So, is there a way we can set the width dynamically?

Upvotes: 0

Views: 419

Answers (0)

Related Questions