Jface dialog with ScrolledComposite

╄→尐↘猪︶ㄣ 提交于 2019-12-13 18:35:55

问题


I am trying to display scrollable composite in my dialog window.

But it do not get the scrollbars. I also don't get the "OK" "Cancel" buttons.

How to fix it?

public class MyDialog extends Dialog {

  public MyDialog (Shell parentShell) {
    super(parentShell);     
  }

   protected void configureShell(Shell newShell) {
    super.configureShell(newShell);
    newShell.setText("test");
    newShell.setSize(200, 100);
  }

  protected Control createDialogArea(Composite parent) {
         ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL |    SWT.V_SCROLL | SWT.BORDER);


    Composite composite = new Composite(sc, SWT.NONE);
    composite.setLayout(new FillLayout(SWT.VERTICAL));

    new Label(composite, SWT.NONE).setText("1111");
    new Label(composite, SWT.NONE).setText("2222");
    new Label(composite, SWT.NONE).setText("3333");
    new Label(composite, SWT.NONE).setText("4444");
    new Label(composite, SWT.NONE).setText("5555");
    new Label(composite, SWT.NONE).setText("6666");
    new Label(composite, SWT.NONE).setText("7777");

    sc.setContent(composite);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);


    return parent;  


  }


回答1:


Ok, so I almost got it working. However, at the bottom there is some empty space where the dialog buttons would be. If this doesn't bother you, or you are going to add buttons, the code below will do what you want. If not, I don't know how to help you.

public class MyDialog extends Dialog
{

    protected MyDialog(Shell parentShell) {
        super(parentShell);
    }

    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("test");
        newShell.setSize(200, 100);
    }

    protected void createButtonsForButtonBar(Composite parent) {
    }

    protected Control createDialogArea(Composite parent) {
        Composite content = (Composite) super.createDialogArea(parent);
        content.setLayout(new FillLayout());

        ScrolledComposite sc = new ScrolledComposite(content, SWT.H_SCROLL
                | SWT.V_SCROLL | SWT.BORDER);

        Composite composite = new Composite(sc, SWT.NONE);
        composite.setLayout(new FillLayout(SWT.VERTICAL));

        new Label(composite, SWT.NONE).setText("1111");
        new Label(composite, SWT.NONE).setText("2222");
        new Label(composite, SWT.NONE).setText("3333");
        new Label(composite, SWT.NONE).setText("4444");
        new Label(composite, SWT.NONE).setText("5555");
        new Label(composite, SWT.NONE).setText("6666");
        new Label(composite, SWT.NONE).setText("7777");

        sc.setContent(composite);
        sc.setExpandHorizontal(true);
        sc.setExpandVertical(true);
        sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

        return parent; 
      }
}

However, I assume you are going to use dialog buttons. Otherwise you could simply use a shell with a composite as seen in the example I posted before...



来源:https://stackoverflow.com/questions/12660842/jface-dialog-with-scrolledcomposite

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