动态调整SWT文本字段的大小

时间:2014-11-01 02:09:42

标签: java eclipse-plugin swt

我有一个SWT文本字段,其内容长度会动态变化。我需要让完整的文字可见。我使用下面的代码来做到这一点。

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

使用此代码,当文本很大时,它会离开屏幕。我无法在运行时调整文本字段的大小。在调整文本框大小后,我还尝试再次调用滚动复合体上的监听器。但它都不起作用。请指出可能出错的地方。

1 个答案:

答案 0 :(得分:3)

当您使用的布局会覆盖您可能在控件上执行的任何setSize

而是将控件的widthHint上的GridData设置为您想要的宽度(也是heightHint)。

您还必须布局整个对话框区域,然后调整外壳的大小。

Composite dialogAreaComp = ... get the dialog area composite

dialogAreaComp.layout(true, true);


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