我有一個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();
}
}
問題是,我有一個行計數器左'的StyledText'作爲'標籤',所以我認爲我仍然需要'ScrolledComposite' ... – Raven
如果你不介意依賴' org.eclipse.jface.text',你可以使用一個'SourceViewer'來包裝一個'StyledText',並允許顯示帶有行號的_rulers_。 –
好吧,你可以在我編輯的問題中看到我發現問題似乎是'ScrolledComposite'而不是'StyledText' ... – Raven