Java – sets the number of lines to display for multiline text in SWT

I am using the following for textarea

ToolBar bar = new ToolBar(@R_532_2419@,SWT.NONE);
ToolItem item = new ToolItem(bar,SWT.SEPARATOR);
Text text = new Text(bar,SWT.BORDER | SWT.MULTI);
item.setWidth(width);
item.setControl(text);

GridData data = new GridData();
data.verticalAlignment = SWT.CENTER;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
text.setLayoutData(data);

I want to display a multiline text box that currently accepts multiple lines of text, but displays only one line at a time@ H_ 404_ 5@

Any idea how to set the number of rows to display@ H_ 404_ 5@

Thank you@ H_ 404_ 5@

Solution

You can set the pixel height:

/* Set the height to 75 pixels */
data.heightHint = 75;

However, you can also set the height according to the number of character lines, but you must do something to measure the character height You need to build a graphical context (GC) to measure the text range@ H_ 404_ 5@

For example: @ h_ 404_ 5@

GC gc = new GC(text);
try
{
    gc.setFont(text.getFont());
    FontMetrics fm = gc.getFontMetrics();

    /* Set the height to 5 rows of characters */
    data.heightHint = 5 * fm.getHeight();
}
finally
{
    gc.dispose();
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>