Does SWT StyledText have a height limit?
I am trying to create an application that contains a StyledText box displayed within a ScrolledComposite. I am having difficulties displaying a large number of lines in my StyledText box (over 2,550 seems to cause issues).
The StyledText box must not itself have a scroll bar but must be scrollable via the ScrolledComposite. As there are other items below and above the StyledText that need to be scrollable to and I do not want multiple scroll bars.
Hence with large amounts of data I have a very large (as in height) StyledText box that seems to stop after a certain height.
The issue is that the StyledText should be as tall is its contents and it is not. The reason for the gap underneath is that the containing composite is resizing what StyledText reports to be its height but this is not in fact its height.
Here is a piece of simplifie开发者_如何学Cd example code to illustrate my issue:
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.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ExpandBox2
{
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Example");
shell.setLayout(new FillLayout());
ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL);
scrolledComposite.setLayout(new FillLayout(SWT.VERTICAL));
Composite mainComp = new Composite(scrolledComposite, SWT.NONE);
mainComp.setLayout(new FillLayout(SWT.VERTICAL));
StyledText styledText = new StyledText(mainComp, SWT.NONE);
styledText.getContent().setText(bigString());
mainComp.setSize(mainComp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
scrolledComposite.setContent(mainComp);
scrolledComposite.setMinSize(mainComp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
scrolledComposite.getVerticalBar().setIncrement(10);
shell.setSize(400, 350);
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) {
display.sleep ();
}
}
display.dispose();
}
private static String bigString()
{
String big = "";
for(int i=0;i<10000;i++)
{
big = big + "hello\r\n";
}
return big;
}
}
Update: Interestingly this problem occurs with SWT Label and SWT Text
This is actually a Windows limitation. Composites may only be of a certain size in windows, no more than 32767 (pixels, I assume).
This is find for the scrolledComposite because it isn't actually > 32767, it just appears to be. Whereas with the mainComp the actual size is > 32767 and this is where we got cut off.
Initially I thought this was an Eclipse bug and filed a report where I was informed that this was a Windows issue/feature: https://bugs.eclipse.org/bugs/show_bug.cgi?id=333111
Perhaps you could solve this problem by going the other way around and place your "other things" inside the StyledText? And then consequently make the StyledText scroll instead of the ScrolledComposite
. StyledText has support for embedding both images and controls, and you are able to implement listeners (for instance VerifyListener
) to keep the user from deleting the embedded object - if that is what you want.
Here's some sample code:
- http://www.java2s.com/Tutorial/Java/0280__SWT/StyledTextembedimages.htm
- http://www.java2s.com/Tutorial/Java/0280__SWT/StyledTextembedcontrols.htm
If you want your controls to look better than in the second example, you can make your controls take up the whole width of the text area (and listen to events for when the area is resized - use styledText.addListener(SWT.Resize, new Listener() ...
).
精彩评论