2013-06-17 2 views
0

SWT StyledText를 사용하여 플러그인에서 내 쿼리에 대한 응답을 채 웁니다. 나는 아래쪽으로 자동 스크롤하는 법을 배웠지 만 마지막 항목을 자동으로 선택하는 정확한 방법을 알아낼 수 없었다. 최적화되지 않은 방식으로이 작업을 수행했습니다. 즉, 이전 줄의 배경을 모두 흰색으로 업데이트 한 다음 최신 추가 줄을 강조 표시했지만 더 나은 방법이 있어야합니다.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** 

감사 :

여기 내 코드입니다!

답변

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