2016-09-12 47 views
1

我有一個StyledText小部件(SWT)在ScrolledComposite內應該顯示日誌文件的內容。不幸的是,日誌文件中有成千上萬行,所以我在達到約2200行後切斷了文本。如何在ScrolledComposite中顯示多行文字?

我發現this post指的是this report,指出窗口中的窗口部件有高度限制,我的理論是我已經達到了這個限制。

我的問題是我如何處理這個問題。顯示多行文字的文字的解決方法是什麼?

編輯:
我發現,這樣做只會發生,如果我用一個StyledText裏面ScrolledComposite。如果我使用普通的StyledText就沒有問題。

下面的代碼重現:

import org.eclipse.swt.SWT; 
import org.eclipse.swt.custom.ScrolledComposite; 
import org.eclipse.swt.custom.StyledText; 
import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 

public class StyledTextLimit { 

    public static void main(String[] args) { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setLayout(new FillLayout()); 

     ScrolledComposite scrollComp = new ScrolledComposite(shell, 
       SWT.H_SCROLL | SWT.V_SCROLL); 

     StyledText text = new StyledText(scrollComp, SWT.NONE); 
     text.setSize(100, 500); 

     scrollComp.setContent(text); 
     scrollComp.setExpandHorizontal(true); 
     scrollComp.setExpandVertical(true); 

     StringBuilder builder = new StringBuilder(); 

     for (int i = 0; i < 5000; i++) { 
      builder.append(i); 

      builder.append(" "); 

      for (int j = 'a'; j < 'a' + 200; j++) { 
       builder.append((char) j); 
      } 

      builder.append("\n"); 
     } 

     text.setText(builder.toString().trim()); 

     scrollComp.setMinSize(text.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 


     // shell.pack(); 
     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 
     display.dispose(); 
    } 

} 

回答

0

我不認爲有必要來包裝StyledTextScrolledComposite。必要時,StyledText會自行顯示滾動條。

我建議使用StyledText而不是ScrolledComposite

StyledText當然也對它能夠容納的文本有限制。然而,這個限制應該遠高於2200線。如果StyledText仍然溢出,那麼您將不得不截斷要顯示的日誌。

+0

問題是,我有一個行計數器左'的StyledText'作爲'標籤',所以我認爲我仍然需要'ScrolledComposite' ... – Raven

+1

如果你不介意依賴' org.eclipse.jface.text',你可以使用一個'SourceViewer'來包裝一個'StyledText',並允許顯示帶有行號的_rulers_。 –

+0

好吧,你可以在我編輯的問題中看到我發現問題似乎是'ScrolledComposite'而不是'StyledText' ... – Raven

0

儘管@RüdigerHerrmann幫助我解決了我的問題,但我仍然覺得我應該幫助那些可能會遇到同樣問題的人,因爲我沒有擺脫ScrolledComposite的可能性。

因此我想鏈接this post處理ScrolledComposite問題。