简体   繁体   中英

How I can add size to text in SWT

I create a label with default text of 'data preview'.

When I tried to change the text ( dnyamic ) to diffrerent text it show me the text only in the size of 'data preview'.

How i can control on the size of the label ?

    final Composite previewHeader = new Composite(this.previewPanel, SWT.NULL);
    final GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    previewHeader.setLayout(layout);
    final GridData headerData = new GridData();
    headerData.horizontalAlignment = GridData.FILL;
    headerData.grabExcessHorizontalSpace = true;
    previewHeader.setLayoutData(headerData);

    this.previewLabel = new RichTextLabel(previewHeader, SWT.LEFT); 
    this.previewLabel.setText("<b>Data Preview:</b>"); //$NON-NLS-1$
    final GridData labeldata = new GridData();
    labeldata.grabExcessHorizontalSpace = true;
    labeldata.verticalAlignment = GridData.VERTICAL_ALIGN_END;
    labeldata.verticalSpan = 0;
    labeldata.verticalIndent = 0;
    this.previewLabel.setLayoutData(labeldata);

You need to call layout() on the parent of the text. Here is the javadoc of Composite#layout() :

If the receiver has a layout, asks the layout to lay out (that is, set the size and location of) the receiver's children. If the receiver does not have a layout, do nothing.

This solves the problem because of the following reason:

The size of each child is determined every time the parent has to lay out. When you first add your label, the parent will initiate the size computation for the child. When you later on change the text, this is not done automatically, so you have to tell the parent to lay out again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM