0

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?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Akki
  • 1
  • 1
  • "The only time it works is when I hard-code the width" - are you referring to the hint that you're giving `scrolledComposite`? – avojak Jul 18 '17 at 19:35
  • I am referring to the value which I would pass in the computeSize(500, SWT.Default) in the setMinSize for scrolled composite. – Akki Jul 18 '17 at 21:28
  • So, I finally solved this issue by configuring the size of the dialog to (500,400) instead and not setting the hint on the grid data for scrolled composite. And later while computing the size I did a little math to fix the issue. scrolledComposite.setMinSize(innerComposite.computeSize( getShell().getClientArea().width - scrolledComposite.getVerticalBar().getSize().x, SWT.DEFAULT)); – Akki Jul 19 '17 at 14:27

0 Answers0