开发者

Specify order/location of swt widgets in a composite object?

I would like to create all my widgets before inserting them into my swt Composite parent. But as I understand the parent is always specified in the constructor when a child is created:

Composite container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
container.se开发者_运维知识库tLayout(layout);
layout.numColumns = 3;
layout.verticalSpacing = 9;
...
// Create a child at first row leftmost location
Label labelConfigurator = new Label(container, SWT.NULL);
labelConfigurator.setText(title);

Is is possible to somehow separate the creation of widgets from the insertion/placement into the parent container?


No, there isn't. There's a little bit of explanation here.

Depending on what you want, a factory might help you; in the past I've used something like this:

public interface ViewerFactory {

    Text getText(Composite parent);

    ContentViewer getListViewer(Composite parent);

    Label getLabel(Composite parent);

}

with implementation

private final class ViewerFactoryImpl implements ViewerFactory {
    @ Override
    public Label getLabel ( Composite parent ) {
        return new Label ( parent , SWT.NONE ) ;
    }

    @ Override
    public ContentViewer getListViewer ( Composite parent ) {
        return new ListViewer ( parent , SWT.V_SCROLL | SWT.BORDER | SWT.MULTI ) ;
    }

    @ Override
    public Text getText ( Composite parent ) {
        return new Text ( parent , SWT.BORDER ) ;
    }

}

...which has helped me abstract away widget-building from most of my code. When I write a unit test, I mock the factory interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜