2013-06-17 2 views
0

Я использую SWT StyledText, чтобы заполнить ответы на мои запросы в моем плагине. Я научился автосканированию до конца, но не смог определить правильный способ автоматического выбора последней записи. Мне удалось сделать это не оптимизированным образом, то есть обновить фон всех предыдущих строк до белого, а затем выделить последнюю добавленную строку, но должен быть лучший способ сделать это.SWT StyledText - auto highlight (или autoselect)

Вот мой код:

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** 

Спасибо!

ответ

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

Это то, что он выглядит следующим образом:

enter image description here

Смежные вопросы