如果文本框中的文本达到限制,我希望增加文本框的高度。事实上,如果文本框的宽度可以容纳最多15个字符,那么在15个字符之后,我的文本框大小应该会增加,这样我就可以看到文本框的两行。我使用的是多行文本框。
发布于 2013-04-03 03:42:08
这是可能的。如果使用SWT.WRAP,当超出文本小部件的行宽时,文本将自动在新行中继续。然而,高度将保持不变。因此,您必须在文本修改事件中计算它。在设置了文本小部件的新高度之后,必须对父级进行布局,以便计算文本小部件同级的新位置。
final Text text = new Text(parent, SWT.MULTI | SWT.BORDER | SWT.WRAP);
text.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
Point textSize = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);
Rectangle textTrim = text.computeTrim(0, 0, textSize.x,
text.getLineHeight());
final int textPadding = textTrim.height - text.getLineHeight();
text.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
int height = text.getLineCount() * text.getLineHeight()
+ textPadding;
text.setSize(text.getSize().x, height);
// need to layout parent, in order to change position of
// siblings
parent.layout();
}
});
https://stackoverflow.com/questions/15760160
复制相似问题