【问题标题】:resize SWT text field dynamically动态调整 SWT 文本字段的大小
【发布时间】:2014-11-01 02:09:42
【问题描述】:

我有一个 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();

            }
        });
    }
}

使用此代码,当文本很大时,它会超出屏幕。我无法在运行时调整文本字段的大小。在调整文本框的大小后,我还尝试再次在滚动合成上调用侦听器。但这些都不起作用。请指出可能出了什么问题。

【问题讨论】:

    标签: java eclipse-plugin swt


    【解决方案1】:

    当您使用将覆盖任何setSize 的布局时,您可以对控件执行此操作。

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

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

    Composite dialogAreaComp = ... get the dialog area composite
    
    dialogAreaComp.layout(true, true);
    
    
    Shell shell = getShell();
    shell.pack(true);
    

    【讨论】:

      猜你喜欢
      • 2013-03-28
      • 2012-11-23
      • 1970-01-01
      • 2015-05-03
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      • 2015-05-13
      相关资源
      最近更新 更多