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.
精彩评论