我正在使用SWT StyledText在我的插件中填充对查询的响应。我学会了如何自动滚动到底部,但无法找出自动选择最后一个条目的正确方法。我已经设法以一种未经优化的方式完成此操作,即将所有先前行的背景更新为白色,然后突出显示最新添加的行,但必须有更好的方法来执行此操作。
这是我的代码:
rspST.append(rsp + "\n"); ** //<--- Appending the new response to the styledText **
rspST.setTopIndex(rspST.getLineCount() - 1); ** //<-- Scroll to bottom**
for(int i=0; i<rspST.getLineCount() - 2; i++) ** //Setting the background of previous entries to white**
{
rspST.setLineBackground(i, 1,rspST.getDisplay().getSystemColor(SWT.COLOR_WHITE));
}
rspST.setLineBackground(rspST.getLineCount() - 2,
1,rspST.getDisplay().getSystemColor(SWT.COLOR_GRAY)); ** Setting the background of the last line added to gray**
谢谢!
答案 0 :(得分:2)
此代码将点击Button
:
private static int lineNr = 0;
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
final StyledText text = new StyledText(shell, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button button = new Button(shell, SWT.PUSH);
button.setText("Add new line");
button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
button.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
/* Get the start position of the new text */
int start = text.getText().length();
/* Create the new line */
String newText = "Line: " + lineNr++ + "\n";
/* Add it */
text.append(newText);
/* Determine the end of the new text */
int end = start + newText.length();
/* Set the selection */
text.setSelection(start, end);
}
});
shell.pack();
shell.setSize(600, 400);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
这就是它的样子: