Does SWT StyledText have a height limit?

霸气de小男生 提交于 2019-12-12 13:35:20

问题


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 simplified 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


回答1:


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




回答2:


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() ...).



来源:https://stackoverflow.com/questions/4510579/does-swt-styledtext-have-a-height-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!