I confirmed that the problem can be solved by the second approach I suggested in the question. If anyone has a better answer, please post.
This approach has these objects:
AbstractLabelWithHeightOfAnotherControl / custom StackLayout
<>-- Other control
Composite / GridLayout
<>-- Label / GridData( SWT.BEGINNING, SWT.CENTER, false, true )
The custom StackLayout has the width of the label, but the height of the other control.
This code provides an abstract class that supports the behavior for a variety of other controls.
public abstract class AbstractLabelWithHeightOfAnotherControl extends Composite {
private Label m_label;
private Control m_otherControl;
/** Constructor.
* @param parent
* @param style
*/
public AbstractLabelWithHeightOfAnotherControl(Composite parent, int style) {
super( parent, style );
StackLayout stackLayout = new MyStackLayout();
this.setLayout( stackLayout );
Composite layerLabel = new Composite( this, SWT.NONE );
GridLayout layerLabelLayout = new GridLayout( 1, false );
layerLabelLayout.marginWidth = 0;
layerLabelLayout.marginHeight = 0;
layerLabel.setLayout( layerLabelLayout );
m_label = new Label( layerLabel, SWT.NONE);
m_label.setLayoutData( new GridData( SWT.BEGINNING, SWT.CENTER, false, true ) );
m_otherControl = makeOtherControl( this );
stackLayout.topControl = layerLabel;
}
protected abstract Control makeOtherControl( @Nonnull Composite parent );
public Label getLabel() {
return m_label;
}
private final class MyStackLayout extends StackLayout {
MyStackLayout() {
this.marginHeight = 0;
this.marginWidth = 0;
}
@Override
protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
int width = m_label.computeSize( wHint, hHint, flushCache ).x;
int height = m_otherControl.computeSize( wHint, hHint, flushCache ).y;
if (wHint != SWT.DEFAULT) width = wHint;
if (hHint != SWT.DEFAULT) height = hHint;
return new Point(width, height);
}
}
}
The implementing class can just provide a method like this:
@Override
protected Control makeOtherControl( @Nonnull Composite parent ) {
return new Combo( parent, SWT.NONE );
}