6

I have ScrolledComposite which allows only vertical scrolling. (heighthint = 400).

Within this ScrolledComposite, I have another CompositeA (height may exceed 400 for scrolling) to store all other widgets.

I have a very long label (with SWT.WRAP enabled). But instead of wrapping, it is always showing in a single line. I want this label to wrap according to the width of its parent (CompositeA)

I forgot to add that this CompositeA is a 2 column GridLayout with makeColumnsEqualWidth = true.

Here is my code:

public void createPartControl(Composite parent) {
    // TODO Auto-generated method stub

    Display display = parent.getDisplay();

    toolkit = new FormToolkit(display);
    form = toolkit.createForm(parent);
    form.setText("ABC");

    Composite body = form.getBody();

    TableWrapLayout layout = new TableWrapLayout();
    layout.numColumns = 2;
    body.setLayout(layout);

    Label header1 = toolkit.createLabel(body, "ABC: ");
    Font font = new Font(display, "Arial", 11, SWT.BOLD);
    header1.setFont(font);

    Label header2 = toolkit.createLabel(body, "XYZ",
            SWT.WRAP);
    font = new Font(display, "Arial", 11, SWT.NONE);
    header2.setFont(font);

    TableWrapData wd = new TableWrapData(TableWrapData.FILL_GRAB);      
    header2.setLayoutData(wd);

    form.getBody().setBackground(
            form.getBody().getDisplay()
                    .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

    // Scrolled composite
    ScrolledComposite sc = new ScrolledComposite(body, SWT.BORDER_SOLID
            | SWT.V_SCROLL);
    sc.setAlwaysShowScrollBars(true);
    sc.setBackground(new Color(display, 50,255,155));


    wd = new TableWrapData(TableWrapData.FILL); 
    wd.heightHint = 360;
    wd.colspan = 2;
    wd.grabHorizontal = false;
    sc.setLayoutData(wd);

    sc.setLayout(new TableWrapLayout());

    Composite innerComposite = toolkit.createComposite(sc);
    sc.setContent(innerComposite);

    innerComposite.setLayout(new TableWrapLayout());
    innerComposite.setBackground(new Color(display, 255,50,50));

    Section section = toolkit.createSection(innerComposite,
            Section.DESCRIPTION | Section.TITLE_BAR | Section.EXPANDED);
    wd = new TableWrapData(TableWrapData.FILL);
    wd.maxWidth = 600; // don't want to hardcode this value

    section.setLayoutData(wd);
    section.setText("Section");
    section.setDescription("A not so long description......................");

    // Composite for Section
    Composite sectionClient = toolkit.createComposite(section);
    layout = new TableWrapLayout();
    layout.numColumns = 2;
    layout.makeColumnsEqualWidth = true;
    sectionClient.setLayout(layout);

    toolkit.createButton(sectionClient, "Button 1", SWT.RADIO);

    Label rightDesc = toolkit
            .createLabel(
                    sectionClient,
                    "A very long long long long long long long long long long long long long long long long long long long long desc that needs wrapping",
                    SWT.WRAP);
    font = new Font(display, "Arial", 10, SWT.ITALIC);
    rightDesc.setFont(font);
    wd = new TableWrapData();
    wd.rowspan = 2;
    rightDesc.setLayoutData(wd);

    Combo comboDropDown = new Combo(sectionClient, SWT.DROP_DOWN
            | SWT.BORDER);
    comboDropDown.setText("DDL");
    comboDropDown.add("1");
    comboDropDown.add("2");
    comboDropDown.add("3");

    Label lineBreak = toolkit.createSeparator(sectionClient, SWT.SEPARATOR
            | SWT.HORIZONTAL);
    wd = new TableWrapData(TableWrapData.FILL);
    wd.colspan = 2;
    lineBreak.setLayoutData(wd);

    /***********************/

    toolkit.createButton(sectionClient, "Button 2", SWT.RADIO);

    Label rightDesc2 = toolkit
            .createLabel(
                    sectionClient,
                    "A long long long long long long long long long long long long long long long long long long long long desc that needs wrapping",
                    SWT.WRAP);
    font = new Font(display, "Arial", 10, SWT.ITALIC);
    rightDesc2.setFont(font);
    wd = new TableWrapData(TableWrapData.FILL);
    wd.rowspan = 3;
    rightDesc2.setLayoutData(wd);

    toolkit.createLabel(sectionClient, "Desc",
            SWT.WRAP);
    toolkit.createText(sectionClient, "hello world", SWT.NONE);

    section.setClient(sectionClient);

    innerComposite.pack();

}

If you run it, you can see a green scrolledcomposite and a red composite. I want the red composite width to fill to the width of the scrolledcomposite relatively without hardcoding maxWidth = 600.

double-beep
  • 5,031
  • 17
  • 33
  • 41
humansg
  • 735
  • 3
  • 12
  • 30
  • 1
    I think I had a similar issue, and I solved it using `FILL` for `horizontalAlignment` and `true` for `grabExcessHorizontalSpace` in the `GridData` of the label and the Composite that contained it. Hope this helps. – Baldrick Aug 02 '12 at 10:05
  • i think when i call `innerComposite.pack()` it will layout the label in a single line? But without `innerComposite.pack()` i will not be able to see the entire layout. – humansg Aug 03 '12 at 02:33

5 Answers5

5

I have the same problem here in my layout and found this useful answer.

Because of 'it is a not bug, it is a feature' try this answer from comment #19 here.

I use the following lines of code for my Label.

Label tip = new Label(shell, SWT.WRAP | SWT.BORDER | SWT.LEFT);
final GridData data = new GridData(SWT.HORIZONTAL, SWT.TOP, true, false, 1, 1);
tip.setLayoutData(data);
tip.setText("Here stands my long, long and much more longer text...");
ryanthara
  • 51
  • 1
  • 3
  • 1
    `new GridData(SWT.HORIZONTAL, ...)` look weird, `HORIZONTAL` is not one of the allowed constants according to the `GridData` documentation. – Lii Sep 20 '16 at 12:37
1

Try adding SWT.WRAP when creating the Label. It worked for me.

Label label = new Label(parent, SWT.WRAP);
dac2009
  • 3,521
  • 1
  • 22
  • 22
0

I might remember this wrong, but I recall I had to re-layout the composite when the text changed. That made the text wrap.

Lii
  • 11,553
  • 8
  • 64
  • 88
dbalakirev
  • 1,918
  • 3
  • 20
  • 31
  • Check Zoltán Ujhelyi's solution here as well: http://stackoverflow.com/questions/3499349/layout-issue-autogrow-label-swt – dbalakirev Aug 02 '12 at 07:22
  • the text did not change. is just that it is already a long text when it first display – humansg Aug 02 '12 at 07:38
  • And you do a layout after you display it first? – dbalakirev Aug 02 '12 at 08:14
  • yes i did a layout for it. the problem is that I do not want to hardcode the width. I want it to automatically follow the ScrolledComposite. But I don't seems to be able to get the width of the SC. Keep returning 0. – humansg Aug 02 '12 at 08:53
  • Interesting. In solution i linked the chap is adding a layoutData setting to the label. Which you don't as i see. – dbalakirev Aug 02 '12 at 09:50
  • Nope. You do actaully. Ignore me :) – dbalakirev Aug 02 '12 at 09:52
  • well.. i have no problem if i did not add it into a scrolled composite. but once i add into a scrolledcomposite, i will have the problem that the label is in a single line if i did not specify the width. but i want things to be relative instead of hardcoded.. – humansg Aug 02 '12 at 10:15
0

It was surprisingly to understand that TableWrapLayout manager will not count on the composites that scrolls horizontally. To keep that layout on the Form toolkit should be used. I've just made an example of code what could be done taking above.

public void createPartControl(Composite parent) {
    // TODO Auto-generated method stub

    Display display = parent.getDisplay();

    toolkit = new FormToolkit(display);
    form = toolkit.createForm(parent);
    form.setText("ABC");

    Composite body = form.getBody();

    TableWrapLayout layout = new TableWrapLayout();
    layout.numColumns = 2;
    body.setLayout(layout);

    Label header1 = toolkit.createLabel(body, "ABC: ");
    Font font = new Font(display, "Arial", 11, SWT.BOLD);
    header1.setFont(font);

    Label header2 = toolkit.createLabel(body, "XYZ", SWT.WRAP);
    font = new Font(display, "Arial", 11, SWT.NONE);
    header2.setFont(font);

    TableWrapData wd = new TableWrapData();      
    header2.setLayoutData(wd);

    body.setBackground(body.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

    // Scrolled composite
    Composite sc = toolkit.createComposite(body, SWT.BORDER_SOLID | SWT.V_SCROLL);
    sc.setBackground(new Color(display, 50,255,155));
    layout = new TableWrapLayout();    
    sc.setLayout(layout);

    wd = new TableWrapData(TableWrapData.FILL_GRAB); 
    wd.heightHint = 360;
    wd.colspan = 2;
    sc.setLayoutData(wd);

    Composite innerComposite = toolkit.createComposite(sc);

    layout = new TableWrapLayout();
    innerComposite.setLayout(layout);
    innerComposite.setBackground(new Color(display, 255,50,50));

    wd = new TableWrapData(TableWrapData.FILL_GRAB); 
    innerComposite.setLayoutData(wd);

    Section section = toolkit.createSection(innerComposite,
            Section.DESCRIPTION | Section.TITLE_BAR | Section.TWISTIE | Section.EXPANDED);

    section.setText("Section");
    section.setDescription("A not so long description......................");

    wd = new TableWrapData(TableWrapData.FILL_GRAB);
    // wd.maxWidth = 600; // don't want to hardcode this value
    section.setLayoutData(wd);


    // Composite for Section
    Composite sectionClient = toolkit.createComposite(section);
    layout = new TableWrapLayout();
    layout.numColumns = 2;
    // layout.makeColumnsEqualWidth = true;
    sectionClient.setLayout(layout);
    section.setClient(sectionClient);

    wd = new TableWrapData(TableWrapData.FILL_GRAB); 
    sectionClient.setLayoutData(wd);

    Label rightDesc = toolkit.createLabel(sectionClient,
                    "A very long long long long long long long long long long long long long long long long long long long long desc that needs wrapping",
                    SWT.WRAP);
    font = new Font(display, "Arial", 10, SWT.ITALIC);
    rightDesc.setFont(font);
    wd = new TableWrapData();
    wd.colspan = 2;
    rightDesc.setLayoutData(wd);

    Combo comboDropDown = new Combo(sectionClient, SWT.DROP_DOWN | SWT.BORDER);
    comboDropDown.setText("DDL");
    comboDropDown.add("1");
    comboDropDown.add("2");
    comboDropDown.add("3");

    Label lineBreak = toolkit.createSeparator(sectionClient, SWT.SEPARATOR | SWT.HORIZONTAL);
    wd = new TableWrapData(TableWrapData.FILL_GRAB);
    lineBreak.setLayoutData(wd);
        //
        //    /***********************/
        //
    Button button1 = toolkit.createButton(sectionClient, "Button 1", SWT.RADIO);
      wd = new TableWrapData();
      wd.colspan = 2;
      button1.setLayoutData(wd);

    Button button2 = toolkit.createButton(sectionClient, "Button 2", SWT.RADIO);
      wd = new TableWrapData();
      wd.colspan = 2;
      button2.setLayoutData(wd);

    Label rightDesc2 = toolkit.createLabel(sectionClient,
                    "A long long long long long long long long long long long long long long long long long long long long desc that needs wrapping",
                    SWT.WRAP);
    font = new Font(display, "Arial", 10, SWT.ITALIC);
    rightDesc2.setFont(font);
    wd = new TableWrapData();
    wd.colspan = 2;
    rightDesc2.setLayoutData(wd);

    Label desc = toolkit.createLabel(sectionClient, "Desc:");
    Text hello = toolkit.createText(sectionClient, "hello world");
    wd = new TableWrapData(TableWrapData.FILL_GRAB);
    hello.setLayoutData(wd);
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

SWT.WRAP is not enough if you want to have the long text label to wrap. The label won't wrap its text by default if you don't specify the widthHint in the layout. This works for me:

Label label = new Label(parent, SWT.WRAP);
label.setText("very very long text....");
GridData gd = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1);
gd.widthHint = 200;
label.setLayoutData(gd);
Dima
  • 4,068
  • 4
  • 38
  • 47