Setting number of rows to be displayed for Multi line text in swt

那年仲夏 提交于 2019-12-10 02:20:46

问题


I am using following for TextArea

ToolBar bar = new ToolBar(box,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 multi line text box, currently its accepting multi line text but showing only a single line at a time.

Any idea how to set the number of rows to be displayed ?

Thanks.


回答1:


You can set the height in pixels:

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

However, you can also set the height in terms of the number of character rows, but you have to do some trickery to measure the character height. You'll need to build a graphics context (GC) to measure the text extent.

For example:

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();
}



回答2:


You can also use Text.getLineHeight() to determine the height of a single line of text, you don't need a GC for that:

final Text text = new Text(container, SWT.BORDER | SWT.WRAP);
GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
gridData.heightHint = 5 * text.getLineHeight();
text.setLayoutData(gridData);


来源:https://stackoverflow.com/questions/9167525/setting-number-of-rows-to-be-displayed-for-multi-line-text-in-swt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!