简体   繁体   中英

ScrolledComposite vertical scrollbar not appearing

I am trying to create a container which will contain a buttons in it after certain amount of buttons I want to get a scroll bar but at the moment scroll bar is not appearing any suggestions?

EDIT

MoBuConLTLUI Class

public class MoBuConLTLUI extends Composite {

      public MoBuConLTLUI(Composite parent) {
          super(parent, SWT.NONE);
          this.setLayout(new GridLayout());
          createScrollableComposite(parent);
      }

      public void createScrollableComposite (Composite parent) {
          // set the size of the scrolled content - method 1
          final ScrolledComposite sc1 = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
          final Composite c1 = new Composite(sc1, SWT.V_SCROLL);
          sc1.setContent(c1);
          sc1.setMinSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
          GridLayout layout = new GridLayout();
          layout.numColumns = 1;
          c1.setLayout(layout);
          Button b1 = new Button (c1, SWT.PUSH);
          b1.setText("first button");
          c1.setSize(200,200);


          Button add = new Button (parent, SWT.PUSH);
          add.setText("add children");
          final int[] index = new int[]{0};
          add.addListener(SWT.Selection, new Listener() {
              public void handleEvent(Event e) {
                  index[0]++;
                  Button button = new Button(c1, SWT.PUSH);
                  button.setText("button "+index[0]);
                  // reset size of content so children can be seen - method 1
                  c1.layout();
              }
          });
     }
}

EDIT The parent is initialized like following

public class TestBundleUI extends AbstractEntryPoint{
   private static final long serialVersionUID = -7954149221017272321L;
   private Composite testUiParentComposite;
   @Override
   public void createContents(Composite parent) {
      this.testUiParentComposite = parent;
      testUiParentComposite.setLayout(new GridLayout());
      new MoBuConLTLUI(parent);
   }
 }

The main thing is that you are missing a

c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));

in the selection listener.

Also you should use SWT.NONE for the c1 Composite , using SWT.V_SCROLL gives an unwanted extra scroll bar.

Since you are not using setExpandVertical there is no point in called setMinSize .

So this works:

final ScrolledComposite sc1 = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
final Composite c1 = new Composite(sc1, SWT.NONE);
sc1.setContent(c1);
final GridLayout layout = new GridLayout();
layout.numColumns = 1;
c1.setLayout(layout);
final Button b1 = new Button (c1, SWT.PUSH);
b1.setText("first button");
c1.setSize(200,200);

final Button add = new Button(parent, SWT.PUSH);
add.setText("add children");
final int[] index = new int[]{0};
add.addListener(SWT.Selection, new Listener() {
    public void handleEvent(final Event e) {
        index[0]++;
        final Button button = new Button(c1, SWT.PUSH);
        button.setText("button "+index[0]);
        // reset size of content so children can be seen - method 1
        c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        c1.layout();
    }
});

There are three things missing from your code:

  1. Your Composite shouldn't use SWT.V_SCROLL as the style. Just use SWT.NONE .

  2. You should call the following on the ScrolledComposite :

     sc1.setExpandHorizontal(true); sc1.setExpandVertical(true); 
  3. Update the min size in the listener:

     c1.layout(); sc1.setMinSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 

Try to change listener implementation.

scrolledComposite.addControlListener(new ControlAdapter() {
            @Override
            public void controlResized(ControlEvent e) {
                scrolledComposite.setMinSize(cs1.computeSize(scrolledComposite.getClientArea().width, SWT.DEFAULT));
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM