Java SWT - ScrolledComposite inside Group

杀马特。学长 韩版系。学妹 提交于 2019-12-25 03:10:26

问题


I have a problem with putting ScrolledComposite inside the Group - it doesn't show inside the Group. ScrolledComposite has to scroll the Label.

final private static Group group_netpbmOutput = new Group(shell, SWT.NONE);
final private static ScrolledComposite scroll_netpbmOutput = new ScrolledComposite(group_netpbmOutput, SWT.V_SCROLL | SWT.H_SCROLL);
final private static Label labelImage_netpbmOutput = new Label(scroll_netpbmOutput, SWT.BORDER);

//init Group
group_netpbmOutput.setLayout(new FillLayout());
formData = new FormData(((FormData)group_options.getLayoutData()).width, 200);
formData.top = new FormAttachment(group_options, 50, SWT.BOTTOM);
formData.left = new FormAttachment(group_options, 0, SWT.LEFT);
group_netpbmOutput.setLayoutData(formData);
group_netpbmOutput.setFont(font_group);
group_netpbmOutput.setText("Netpbm output");

//init Label
labelImage_netpbmOutput.setBackgroundImage(new Image(...));

//init ScrollComposite
scroll_netpbmOutput.setLayout(new FillLayout());
scroll_netpbmOutput.setContent(labelImage_netpbmOutput);

EDIT: reaction to Baz's answer

That works, but...the picture is showed this way (there should be only one 'stackoverflow'):

final private static Group group_netpbmOutput = new Group(shell, SWT.NONE);
final private static ScrolledComposite scroll_netpbmOutput = new ScrolledComposite(group_netpbmOutput, SWT.V_SCROLL | SWT.H_SCROLL);
final private static Label labelImage_netpbmOutput = new Label(scroll_netpbmOutput, SWT.BORDER);

group_netpbmOutput.setLayout(new GridLayout());
formData = new FormData(((FormData)group_options.getLayoutData()).width, 200);
formData.top = new FormAttachment(group_options, 50, SWT.BOTTOM);
formData.left = new FormAttachment(group_options, 0, SWT.LEFT);
group_netpbmOutput.setLayoutData(formData);
group_netpbmOutput.setFont(font_group);
group_netpbmOutput.setText("Netpbm output");

labelImage_netpbmOutput.setBackgroundImage(new Image(...));

scroll_netpbmOutput.setLayout(new GridLayout(1, false));
scroll_netpbmOutput.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

scroll_netpbmOutput.setContent(labelImage_netpbmOutput);
scroll_netpbmOutput.setExpandHorizontal(true);
scroll_netpbmOutput.setExpandVertical(true);
scroll_netpbmOutput.setMinSize(labelImage_netpbmOutput.computeSize(SWT.DEFAULT, SWT.DEFAULT));

回答1:


Not quite sure why your code isn't working. It's maybe because you're not setting the min-size of the ScrolledComposite via ScrolledComposite#setMinSize(Point).

This definitely works just fine though:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell();
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, false));

    Group group = new Group(shell, SWT.NONE);
    group.setText("Group");
    group.setLayout(new GridLayout(1, false));
    group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    ScrolledComposite scroll = new ScrolledComposite(group, SWT.V_SCROLL | SWT.H_SCROLL);
    scroll.setLayout(new GridLayout(1, false));
    scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Image image = new Image(display, "stackoverflow.png");

    Label label = new Label(scroll, SWT.NONE);
    label.setImage(image);

    scroll.setContent(label);
    scroll.setExpandHorizontal(true);
    scroll.setExpandVertical(true);
    scroll.setMinSize(label.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    shell.pack();
    shell.setSize(200, 200);
    shell.open();

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

    image.dispose();
}

Looks like this:



来源:https://stackoverflow.com/questions/25461240/java-swt-scrolledcomposite-inside-group

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