How to do scroll scrolling in an Eclipse RCP application?

What I have tried so far:

In createPartControl:

ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL); sc.setLayoutData(new GridData(GridData.FILL_BOTH)); sc.setExpandVertical(true); sc.setExpandHorizontal(true); sc.setSize(ApplicationWorkbenchWindowAdvisor.WIDTH, ApplicationWorkbenchWindowAdvisor.HEIGHT); final TabFolder tabFolder = new TabFolder(sc, SWT.TOP); 

but it does not work. My problem is that if I resize the program window, the scroll bars do not appear in my view. Any ideas?

+6
source share
3 answers

The Javadoc of ScrolledComposite describes two ways to use it, including sample code. Summarizing:

  • You either set the size of the control / composite contained in your ScrolledComposite on the control / composite itself
  • Or you specify your ScrolledComposite minimum size for its contents.

You are currently not doing anything. You set the size to ScrolledComposite , but if you don't use the layout manager, that doesn't make much sense. In any case, see the link above for some official code examples.

+7
source

Usually in Eclipse views, I want my controls to grab all the available space and display only the scroll bars, if otherwise the control will be reduced below the allowable size.

Other answers are perfectly valid, but I would like to add a complete example of the createPartControl method (Eclipse e4).

 @PostConstruct public void createPartControl(Composite parent) { ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL); Composite composite = new Composite(sc, SWT.NONE); sc.setContent(composite); composite.setLayout(new GridLayout(2, false)); Label label = new Label(composite, SWT.NONE); label.setText("Foo"); Text text = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI); GridDataFactory.fillDefaults().grab(true, true).hint(400, 400).applyTo(text); sc.setExpandHorizontal(true); sc.setExpandVertical(true); sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); } 

Note that .fillDefaults() implies .align(SWT.FILL, SWT.FILL) .

I usually use this template, so I created the following small helper method:

 public static ScrolledComposite createScrollable(Composite parent, Consumer<Composite> scrollableContentCreator) { ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); Composite composite = new Composite(sc, SWT.NONE); sc.setContent(composite); scrollableContentCreator.accept(composite); sc.setExpandHorizontal(true); sc.setExpandVertical(true); sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); return sc; } 

Thanks to Java 8 lambdas, you can now implement new scrollable composites in a very compact way:

  createScrollable(container, composite -> { composite.setLayout(new FillLayout()); // fill composite with controls }); 
+6
source

This is a small piece of code that worked for me:

  ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); Composite composite = new Composite(sc, SWT.NONE); sc.setContent(composite); Label lblRelation = new Label(composite, SWT.NONE); lblRelation.setBounds(10, 13, 74, 15); lblRelation.setText("Label name:"); composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 
+4
source

Source: https://habr.com/ru/post/899440/


All Articles