Scrollable Composite - auto resize - swt

eclipse-rcp, java, jface, swt

Solution

The following code will make the `ScrolledComposite` set the `minSize` according to the content and show the scrollbars if you decrease the size of the `Shell`:

ScrolledComposite sc = new ScrolledComposite(content, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);

Composite composite = new Composite(sc, SWT.NONE);
composite.setLayout(new FillLayout(SWT.VERTICAL));

new Label(composite, SWT.NONE).setText("1111");
new Label(composite, SWT.NONE).setText("2222");
new Label(composite, SWT.NONE).setText("3333");
new Label(composite, SWT.NONE).setText("4444");
new Label(composite, SWT.NONE).setText("5555");
new Label(composite, SWT.NONE).setText("6666");
new Label(composite, SWT.NONE).setText("7777");

sc.setContent(composite);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

Problem

I have a Application Window using a Scrollable Composite. Inside scrollable composite we could have N composites (accordingly database data). How could I detected the size of children's composites to set the correct height? ``` /** * Create contents of the application window. * @param parent */ @Override protected Control createContents(Composite parent) { final ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.BORDER); final Composite containerEditarAtendimento = new Composite(sc, SWT.BORDER); containerEditarAtendimento.setSize(800, 630); sc.setContent(containerEditarAtendimento); sc.setExpandHorizontal(true); sc.setExpandVertical(true); sc.setMinSize(containerEditarAtendimento.computeSize(820, 480)); GridLayout gridLayout = new GridLayout(1, false); gridLayout.marginTop = 5; gridLayout.marginRight = 5; gridLayout.marginBottom = 5; gridLayout.marginLeft = 5; containerEditarAtendimento.setLayout(gridLayout); { // Composite com as informações de atendimento. EditarAtendimentoComposite editarAtendimentoComposite = new EditarAtendimentoComposite(containerEditarAtendimento, SWT.BORDER); GridData gd_editarAtendimentoComposite = new GridData(GridData.FILL, GridData.FILL, true, false); gd_editarAtendimentoComposite.heightHint = 249; editarAtendimentoComposite.setLayoutData(gd_editarAtendimentoComposite); } { Composite compoExterno = formToolkit.createComposite(containerEditarAtendimento, SWT.BORDER); compoExterno.setLayout(new GridLayout(3, false)); GridData gd_compoExterno = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1); gd_compoExterno.heightHint = 153; compoExterno.setLayoutData(gd_compoExterno); formToolkit.paintBordersFor(compoExterno); { Label lblAnexos = formToolkit.createLabel(compoExterno, getLabel("label.anexos"), SWT.NONE); } new Label(compoExterno, SWT.NONE); { Button btnAddAnexo = formToolkit.createButton(compoExterno, getLabel("label.incluiranexo"), SWT.NONE); btnAddAnexo.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); } { // Composite com os anexos AnexosComposite anexosComposite = new AnexosComposite(compoExterno,SWT.BORDER); GridData gd_anexosComposite = new GridData(SWT.FILL, SWT.TOP, true, false, 3, 1); gd_anexosComposite.heightHint = 107; anexosComposite.setLayoutData(gd_anexosComposite); } } { // Composite onde ficará o expandable do diagnostico PlanoAcaoExternoComposite planoAcaoComposite = new PlanoAcaoExternoComposite(containerEditarAtendimento, SWT.BORDER); GridData gd_planoAcaoComposite = new GridData(GridData.FILL, GridData.FILL, true, false); planoAcaoComposite.setLayoutData(gd_planoAcaoComposite); formToolkit.adapt(planoAcaoComposite); formToolkit.paintBordersFor(planoAcaoComposite); planoAcaoComposite.layout(true, true); log.info("Height: {}",planoAcaoComposite.getClientArea().height); log.info("getSize().x: {}",planoAcaoComposite.getSize().x); log.info("getSize().y: {}",planoAcaoComposite.getSize().y); sc.setMinSize(containerEditarAtendimento.computeSize(800, 2000)); // HOW TO DYNAMICALLY SET HEIGHT? } sc.layout(true,true); return containerEditarAtendimento; } ```

Original source