如何包装swt标签文本

时间:2015-09-09 15:50:38

标签: java swt eclipse-rcp

我试图自动包装这个文本,但它没有包装。我尝试使用下面的代码,不确定代码有什么问题。这是

protected Control createContents(Composite parent){
    Composite composite = new Composite(parent, SWT.WRAP);
    composite.setLayout(new GridLayout());
    composite.setLayoutData(new GridData(SWT.HORIZONTAL, SWT.TOP, false, false));

    Group group = new Group(composite, SWT.NONE);
    group.setText("my group");
    group.setLayout(new GridLayout(2, false));
    group.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    Button button = new Button(group2,SWT.CHECK);
    GridData data = new GridData(SWT.HORIZONTAL, SWT.TOP, false, true, 1, 1);
    button.setLayoutData(data);

    Label label= new Label(group, SWT.WRAP);
    data = new GridData(GridData.FILL_HORIZONTAL);
    label.setLayoutData(data);
    label.setText("my long text is very long, I need to wrap this very long text lalalalalalalalalalalalalala");
}

1 个答案:

答案 0 :(得分:3)

复合的layoutdata也需要将grabExcessHorizontal空间设置为true,即:

composite.setLayoutData(new GridData(SWT.NONE, SWT.TOP, true, false));

我认为包装标签及其所有父组合需要具有此功能。

顺便说一下,样式位SWT.WRAP对于类Composite来说没有意义。