Raven Raven - 2 months ago 14
Java Question

How to display text with many lines inside a ScrolledComposite?

I have a

StyledText
widget (SWT) inside a
ScrolledComposite
that should display the content of a log file. Unfortunately the log file has thousands of lines in it so that I came to the point where the widget cuts off the text after ~ 2200 lines.

I found this post that refers to this report that states that there is a height limitation for widgets in windows and my theory is that I have reached that limit.

My question is how I can deal with this. What is the workaround for displaying text with that many lines in it?

EDIT:

I found out that this does only happen, if I use the
StyledText
inside a
ScrolledComposite
. If I use the plain
StyledText
there is no problem.

Here's the code to reproduce:

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

}

Answer

I don't see a need to wrap the StyledText into a ScrolledComposite. The StyledText shows scrollbars on its when necessary.

I suggest using the StyledText without a ScrolledComposite.

The StyledText certainly also has a limit on what text it is capable to hold. This limit, however, should be much higher than 2200 lines. If StyledText still overflows, then you'll have to truncate the log to be shown.

Comments