将滚动内容放在组中

时间:2018-01-03 06:40:29

标签: java swt

在下面的代码中,scroll设置在Group之外,如何将其置于Group之内?

protected Control createDialogArea(Composite parent) {
    composite = (Composite) super.createDialogArea(parent);
    ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);
    scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    scrolledComposite.setExpandHorizontal(true);
    scrolledComposite.setExpandVertical(true);
    Composite innerComposite = new Composite(scrolledComposite, SWT.NONE);
    innerComposite.setLayout(new GridLayout());

    grpModelProperties1 = new Group(innerComposite, SWT.SHADOW_IN);
    grpModelProperties1.setLayout(new GridLayout());
    grpModelProperties1.setText("Test Model");
    GridData data1 = new GridData(SWT.FILL, SWT.TOP, true, false);
    data1.heightHint = 200;
    grpModelProperties1.setLayoutData(data1);

    scrolledComposite.setContent(innerComposite);
    scrolledComposite.setMinSize(innerComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));  
    drawWitness(model);
    return composite;
 }

 private void drawWitness(IWitnessModel witness) {
    IWitnessVisualizer visualizer = WitnessUtil.getWitnessVisualizer(witness);
    for (Control ctrl : grpModelProperties1.getChildren()) {
        ctrl.dispose();
    }
    visualizer.render(grpModelProperties1);
    grpModelProperties1.pack();
}

1 个答案:

答案 0 :(得分:1)

目前,您正在GroupinnerComposite内创建ScrolledComposite

Composite innerComposite = new Composite(scrolledComposite, SWT.NONE);
...
grpModelProperties1 = new Group(innerComposite, SWT.SHADOW_IN);

如果您希望滚动显示在Group内,只需在ScrolledComposite上创建Group

protected Control createDialogArea(Composite parent) {
    composite = (Composite) super.createDialogArea(parent);

    // Create the Group on the top level Composite
    grpModelProperties1 = new Group(composite, SWT.SHADOW_IN);
    GridData data1 = new GridData(SWT.FILL, SWT.TOP, true, false);
    data1.heightHint = 200;
    grpModelProperties1.setLayoutData(data1);
    grpModelProperties1.setLayout(new GridLayout());
    grpModelProperties1.setText("Test Model");

    // Within the Group, create the ScrolledComposite
    ScrolledComposite scrolledComposite = new ScrolledComposite(grpModelProperties1, SWT.H_SCROLL | SWT.V_SCROLL);
    scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    scrolledComposite.setExpandHorizontal(true);
    scrolledComposite.setExpandVertical(true);
    // Create the scrolled content (the inner composite)
    Composite innerComposite = new Composite(scrolledComposite, SWT.NONE);
    innerComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    innerComposite.setLayout(new GridLayout());

    // Some misc. data to test the scrolling
    final Label label = new Label(innerComposite, SWT.NONE);
    label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    label.setText("a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr");

    scrolledComposite.setContent(innerComposite);
    scrolledComposite.setMinSize(innerComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    return composite;
}

enter image description here