Krishnaveni
Krishnaveni

Reputation: 809

resize SWT text field dynamically

I have an SWT text field, whose content length will vary dynamically. I need to make the complete text visible. I am using the below code to do that.

public class MyDynamicDialog extends TrayDialog {
     Text   errorMessageText;
     ScrolledComposite scrolledComposite;
     Shell  shell;
     DynamicData  dynamicData;

     public MyDynamicDialog (final Shell shell) {
        super(shell);
        this.shell = shell;
     }
     public MyDynamicDialog (final Shell shell, final DynamicData dynamicData) {
        super(shell);
        this.shell = shell;     
        this.dynamicData = dynamicData;
    }

    @Override
    protected Control createDialogArea(final Composite parent) {
        final GridLayout layout = new GridLayout();
        parent.setLayout(layout);
        this.scrolledComposite = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.BORDER);
        this.scrolledComposite.setExpandHorizontal(true);
        this.scrolledComposite.setExpandVertical(false);

        final Composite composite = new Composite(this.scrolledComposite, SWT.NONE);
        final GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        composite.setLayout(gridLayout);
        composite.setSize(400, 400);

        this.scrolledComposite.setContent(composite);

        this.scrolledComposite.addControlListener(new ControlAdapter() {
            @Override
            public void controlResized(final ControlEvent e) {
                final Rectangle clientArea =       
                MyDynamicDialog.this.scrolledComposite.getClientArea();
                final Point minSize = MyDynamicDialog.this.scrolledComposite.getContent().computeSize(clientArea.width, SWT.DEFAULT);
                MyDynamicDialog.this.scrolledComposite.getContent().setSize(minSize);
            }
        });
        createErrorMessageText(composite);
        return parent;
    }

    private void createErrorMessageText(final Composite parent) {
        final GridData errText = new GridData(/*200, SWT.DEFAULT*/);
        errText.grabExcessHorizontalSpace = true;
        errText.horizontalSpan = 3;
        errText.horizontalAlignment = GridData.FILL;
        this.errorMessageText = new Text(parent, SWT.READ_ONLY /*| SWT.WRAP | SWT.MULTI*/);
        this.errorMessageText.setLayoutData(errText);

        this.errorMessageText.addModifyListener(new ModifyListener() {
            public void modifyText(final ModifyEvent e) {

                final int columns = MyDynamicDialog.this.errorMessageText.getText().length() + 3;
                final GC gc = new GC (MyDynamicDialog.this.errorMessageText);
                final FontMetrics fm = gc.getFontMetrics ();
                final int width = columns * fm.getAverageCharWidth ();
                final int height = fm.getHeight ();
                gc.dispose ();
                MyDynamicDialog.this.errorMessageText.setSize  (MyDynamicDialog.this.errorMessageText.computeSize (width, height));
                MyDynamicDialog.this.errorMessageText.redraw();
                MyDynamicDialog.this.errorMessageText.getParent().layout();

            }
        });
    }
}

enter image description here

With this code, when the text is big, it goes out of the screen. I m not able to resize the text field in runtime. I also tried calling the listener on the scrolled composite, again, after resizing the text box. But none of it works. Please point out what could be wrong.

Upvotes: 1

Views: 3486

Answers (1)

greg-449
greg-449

Reputation: 111142

When you use a Layout that will override any setSize you may do on a control.

Instead set the widthHint on the GridData for the control to the width you want (also heightHint).

You will also have to layout the entire dialog area and then resize the shell.

Composite dialogAreaComp = ... get the dialog area composite

dialogAreaComp.layout(true, true);


Shell shell = getShell();
shell.pack(true);

Upvotes: 3

Related Questions